OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /* This library defines the operations that define and manipulate Dart | |
6 * classes. Included in this are: | |
7 * - Generics | |
8 * - Class metadata | |
9 * - Extension methods | |
10 */ | |
11 | |
12 // TODO(leafp): Consider splitting some of this out. | |
13 dart_library.library('dart/_classes', null, /* Imports */[ | |
14 ], /* Lazy Imports */[ | |
15 'dart/_utils', | |
16 'dart/core', | |
17 'dart/_interceptors', | |
18 'dart/_types', | |
19 'dart/_rtti', | |
20 ], function(exports, dart_utils, core, _interceptors, types, rtti) { | |
21 'use strict'; | |
22 | |
23 const assert = dart_utils.assert; | |
24 const copyProperties = dart_utils.copyProperties; | |
25 const copyTheseProperties = dart_utils.copyTheseProperties; | |
26 const defineMemoizedGetter = dart_utils.defineMemoizedGetter; | |
27 const safeGetOwnProperty = dart_utils.safeGetOwnProperty; | |
28 const throwInternalError = dart_utils.throwInternalError; | |
29 | |
30 const defineProperty = Object.defineProperty; | |
31 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | |
32 const getOwnPropertySymbols = Object.getOwnPropertySymbols; | |
33 | |
34 /** The Symbol for storing type arguments on a specialized generic type. */ | |
35 const _mixins = Symbol('mixins'); | |
36 const _implements = Symbol('implements'); | |
37 exports.implements = _implements; | |
38 const _metadata = Symbol('metadata'); | |
39 exports.metadata = _metadata; | |
40 | |
41 /** | |
42 * Returns a new type that mixes members from base and all mixins. | |
43 * | |
44 * Each mixin applies in sequence, with further to the right ones overriding | |
45 * previous entries. | |
46 * | |
47 * For each mixin, we only take its own properties, not anything from its | |
48 * superclass (prototype). | |
49 */ | |
50 function mixin(base, ...mixins) { | |
51 // Create an initializer for the mixin, so when derived constructor calls | |
52 // super, we can correctly initialize base and mixins. | |
53 | |
54 // Create a class that will hold all of the mixin methods. | |
55 class Mixin extends base { | |
56 // Initializer method: run mixin initializers, then the base. | |
57 [base.name](...args) { | |
58 // Run mixin initializers. They cannot have arguments. | |
59 // Run them backwards so most-derived mixin is initialized first. | |
60 for (let i = mixins.length - 1; i >= 0; i--) { | |
61 let mixin = mixins[i]; | |
62 let init = mixin.prototype[mixin.name]; | |
63 if (init) init.call(this); | |
64 } | |
65 // Run base initializer. | |
66 let init = base.prototype[base.name]; | |
67 if (init) init.apply(this, args); | |
68 } | |
69 } | |
70 // Copy each mixin's methods, with later ones overwriting earlier entries. | |
71 for (let m of mixins) { | |
72 copyProperties(Mixin.prototype, m.prototype); | |
73 } | |
74 | |
75 // Set the signature of the Mixin class to be the composition | |
76 // of the signatures of the mixins. | |
77 setSignature(Mixin, { | |
78 methods: () => { | |
79 let s = {}; | |
80 for (let m of mixins) { | |
81 copyProperties(s, m[_methodSig]); | |
82 } | |
83 return s; | |
84 } | |
85 }); | |
86 | |
87 // Save mixins for reflection | |
88 Mixin[_mixins] = mixins; | |
89 return Mixin; | |
90 } | |
91 exports.mixin = mixin; | |
92 | |
93 function getMixins (clazz) { | |
94 return clazz[_mixins]; | |
95 } | |
96 exports.getMixins = getMixins; | |
97 | |
98 function getImplements (clazz) { | |
99 return clazz[_implements]; | |
100 } | |
101 exports.getImplements = getImplements; | |
102 | |
103 /** The Symbol for storing type arguments on a specialized generic type. */ | |
104 let _typeArguments = Symbol('typeArguments'); | |
105 let _originalDeclaration = Symbol('originalDeclaration'); | |
106 | |
107 /** Memoize a generic type constructor function. */ | |
108 function generic(typeConstructor) { | |
109 let length = typeConstructor.length; | |
110 if (length < 1) { | |
111 throwInternalError('must have at least one generic type argument'); | |
112 } | |
113 let resultMap = new Map(); | |
114 function makeGenericType(...args) { | |
115 if (args.length != length && args.length != 0) { | |
116 throwInternalError('requires ' + length + ' or 0 type arguments'); | |
117 } | |
118 while (args.length < length) args.push(types.dynamic); | |
119 | |
120 let value = resultMap; | |
121 for (let i = 0; i < length; i++) { | |
122 let arg = args[i]; | |
123 if (arg == null) { | |
124 throwInternalError('type arguments should not be null: ' | |
125 + typeConstructor); | |
126 } | |
127 let map = value; | |
128 value = map.get(arg); | |
129 if (value === void 0) { | |
130 if (i + 1 == length) { | |
131 value = typeConstructor.apply(null, args); | |
132 // Save the type constructor and arguments for reflection. | |
133 if (value) { | |
134 value[_typeArguments] = args; | |
135 value[_originalDeclaration] = makeGenericType; | |
136 } | |
137 } else { | |
138 value = new Map(); | |
139 } | |
140 map.set(arg, value); | |
141 } | |
142 } | |
143 return value; | |
144 } | |
145 return makeGenericType; | |
146 } | |
147 exports.generic = generic; | |
148 | |
149 function getGenericClass(type) { | |
150 return safeGetOwnProperty(type, _originalDeclaration); | |
151 }; | |
152 exports.getGenericClass = getGenericClass; | |
153 | |
154 function getGenericArgs(type) { | |
155 return safeGetOwnProperty(type, _typeArguments); | |
156 }; | |
157 exports.getGenericArgs = getGenericArgs; | |
158 | |
159 let _constructorSig = Symbol('sigCtor'); | |
160 let _methodSig = Symbol("sig"); | |
161 let _staticSig = Symbol("sigStatic"); | |
162 | |
163 /// Get the type of a method using the stored signature | |
164 function _getMethodType(obj, name) { | |
165 if (obj === void 0) return void 0; | |
166 if (obj == null) return void 0; | |
167 let sigObj = obj.__proto__.constructor[_methodSig]; | |
168 if (sigObj === void 0) return void 0; | |
169 let parts = sigObj[name]; | |
170 if (parts === void 0) return void 0; | |
171 return types.definiteFunctionType.apply(null, parts); | |
172 } | |
173 | |
174 /// Get the type of a constructor from a class using the stored signature | |
175 /// If name is undefined, returns the type of the default constructor | |
176 /// Returns undefined if the constructor is not found. | |
177 function _getConstructorType(cls, name) { | |
178 if(!name) name = cls.name; | |
179 if (cls === void 0) return void 0; | |
180 if (cls == null) return void 0; | |
181 let sigCtor = cls[_constructorSig]; | |
182 if (sigCtor === void 0) return void 0; | |
183 let parts = sigCtor[name]; | |
184 if (parts === void 0) return void 0; | |
185 return types.definiteFunctionType.apply(null, parts); | |
186 } | |
187 exports.classGetConstructorType = _getConstructorType; | |
188 | |
189 /// Given an object and a method name, tear off the method. | |
190 /// Sets the runtime type of the torn off method appropriately, | |
191 /// and also binds the object. | |
192 /// | |
193 /// If the optional `f` argument is passed in, it will be used as the method. | |
194 /// This supports cases like `super.foo` where we need to tear off the method | |
195 /// from the superclass, not from the `obj` directly. | |
196 /// TODO(leafp): Consider caching the tearoff on the object? | |
197 function bind(obj, name, f) { | |
198 if (f === void 0) f = obj[name]; | |
199 f = f.bind(obj); | |
200 // TODO(jmesserly): track the function's signature on the function, instead | |
201 // of having to go back to the class? | |
202 let sig = _getMethodType(obj, name); | |
203 assert(sig); | |
204 rtti.tag(f, sig); | |
205 return f; | |
206 } | |
207 exports.bind = bind; | |
208 | |
209 // Set up the method signature field on the constructor | |
210 function _setMethodSignature(f, sigF) { | |
211 defineMemoizedGetter(f, _methodSig, () => { | |
212 let sigObj = sigF(); | |
213 sigObj.__proto__ = f.__proto__[_methodSig]; | |
214 return sigObj; | |
215 }); | |
216 } | |
217 | |
218 // Set up the constructor signature field on the constructor | |
219 function _setConstructorSignature(f, sigF) { | |
220 defineMemoizedGetter(f, _constructorSig, sigF); | |
221 } | |
222 | |
223 // Set up the static signature field on the constructor | |
224 function _setStaticSignature(f, sigF) { | |
225 defineMemoizedGetter(f, _staticSig, sigF); | |
226 } | |
227 | |
228 // Set the lazily computed runtime type field on static methods | |
229 function _setStaticTypes(f, names) { | |
230 for (let name of names) { | |
231 rtti.tagMemoized(f[name], function() { | |
232 let parts = f[_staticSig][name]; | |
233 return types.definiteFunctionType.apply(null, parts); | |
234 }) | |
235 } | |
236 } | |
237 | |
238 /// Set up the type signature of a class (constructor object) | |
239 /// f is a constructor object | |
240 /// signature is an object containing optional properties as follows: | |
241 /// methods: A function returning an object mapping method names | |
242 /// to method types. The function is evaluated lazily and cached. | |
243 /// statics: A function returning an object mapping static method | |
244 /// names to types. The function is evalutated lazily and cached. | |
245 /// names: An array of the names of the static methods. Used to | |
246 /// permit eagerly setting the runtimeType field on the methods | |
247 /// while still lazily computing the type descriptor object. | |
248 function setSignature(f, signature) { | |
249 let constructors = | |
250 ('constructors' in signature) ? signature.constructors : () => ({}); | |
251 let methods = | |
252 ('methods' in signature) ? signature.methods : () => ({}); | |
253 let statics = | |
254 ('statics' in signature) ? signature.statics : () => ({}); | |
255 let names = | |
256 ('names' in signature) ? signature.names : []; | |
257 _setConstructorSignature(f, constructors); | |
258 _setMethodSignature(f, methods); | |
259 _setStaticSignature(f, statics); | |
260 _setStaticTypes(f, names); | |
261 rtti.tagMemoized(f, () => core.Type); | |
262 } | |
263 exports.setSignature = setSignature; | |
264 | |
265 function hasMethod(obj, name) { | |
266 return _getMethodType(obj, name) !== void 0; | |
267 } | |
268 exports.hasMethod = hasMethod; | |
269 | |
270 exports.getMethodType = _getMethodType; | |
271 | |
272 /** | |
273 * This is called whenever a derived class needs to introduce a new field, | |
274 * shadowing a field or getter/setter pair on its parent. | |
275 * | |
276 * This is important because otherwise, trying to read or write the field | |
277 * would end up calling the getter or setter, and one of those might not even | |
278 * exist, resulting in a runtime error. Even if they did exist, that's the | |
279 * wrong behavior if a new field was declared. | |
280 */ | |
281 function virtualField(subclass, fieldName) { | |
282 // If the field is already overridden, do nothing. | |
283 let prop = getOwnPropertyDescriptor(subclass.prototype, fieldName); | |
284 if (prop) return; | |
285 | |
286 let symbol = Symbol(subclass.name + '.' + fieldName); | |
287 defineProperty(subclass.prototype, fieldName, { | |
288 get: function() { return this[symbol]; }, | |
289 set: function(x) { this[symbol] = x; } | |
290 }); | |
291 } | |
292 exports.virtualField = virtualField; | |
293 | |
294 /** | |
295 * Given a class and an initializer method name, creates a constructor | |
296 * function with the same name. For example `new SomeClass.name(args)`. | |
297 */ | |
298 function defineNamedConstructor(clazz, name) { | |
299 let proto = clazz.prototype; | |
300 let initMethod = proto[name]; | |
301 let ctor = function() { return initMethod.apply(this, arguments); }; | |
302 ctor.prototype = proto; | |
303 // Use defineProperty so we don't hit a property defined on Function, | |
304 // like `caller` and `arguments`. | |
305 defineProperty(clazz, name, { value: ctor, configurable: true }); | |
306 } | |
307 exports.defineNamedConstructor = defineNamedConstructor; | |
308 | |
309 let _extensionType = Symbol('extensionType'); | |
310 | |
311 let dartx = {}; | |
312 exports.dartx = dartx; | |
313 | |
314 function getExtensionSymbol(name) { | |
315 let sym = dartx[name]; | |
316 if (!sym) dartx[name] = sym = Symbol('dartx.' + name); | |
317 return sym; | |
318 } | |
319 | |
320 function defineExtensionNames(names) { | |
321 names.forEach(getExtensionSymbol); | |
322 } | |
323 exports.defineExtensionNames = defineExtensionNames; | |
324 | |
325 /** | |
326 * Copy symbols from the prototype of the source to destination. | |
327 * These are the only properties safe to copy onto an existing public | |
328 * JavaScript class. | |
329 */ | |
330 function registerExtension(jsType, dartExtType) { | |
331 let extProto = dartExtType.prototype; | |
332 let jsProto = jsType.prototype; | |
333 | |
334 // Mark the JS type's instances so we can easily check for extensions. | |
335 assert(jsProto[_extensionType] === void 0); | |
336 jsProto[_extensionType] = extProto; | |
337 | |
338 let dartObjProto = core.Object.prototype; | |
339 while (extProto !== dartObjProto && extProto !== jsProto) { | |
340 copyTheseProperties(jsProto, extProto, getOwnPropertySymbols(extProto)); | |
341 extProto = extProto.__proto__; | |
342 } | |
343 let originalSigFn = getOwnPropertyDescriptor(dartExtType, _methodSig).get; | |
344 assert(originalSigFn); | |
345 defineMemoizedGetter(jsType, _methodSig, originalSigFn); | |
346 } | |
347 exports.registerExtension = registerExtension; | |
348 | |
349 /** | |
350 * Mark a concrete type as implementing extension methods. | |
351 * For example: `class MyIter implements Iterable`. | |
352 * | |
353 * This takes a list of names, which are the extension methods implemented. | |
354 * It will add a forwarder, so the extension method name redirects to the | |
355 * normal Dart method name. For example: | |
356 * | |
357 * defineExtensionMembers(MyType, ['add', 'remove']); | |
358 * | |
359 * Results in: | |
360 * | |
361 * MyType.prototype[dartx.add] = MyType.prototype.add; | |
362 * MyType.prototype[dartx.remove] = MyType.prototype.remove; | |
363 */ | |
364 // TODO(jmesserly): essentially this gives two names to the same method. | |
365 // This benefit is roughly equivalent call performance either way, but the | |
366 // cost is we need to call defineExtensionMembers any time a subclass | |
367 // overrides one of these methods. | |
368 function defineExtensionMembers(type, methodNames) { | |
369 let proto = type.prototype; | |
370 for (let name of methodNames) { | |
371 let method = getOwnPropertyDescriptor(proto, name); | |
372 defineProperty(proto, getExtensionSymbol(name), method); | |
373 } | |
374 // Ensure the signature is available too. | |
375 // TODO(jmesserly): not sure if we can do this in a cleaner way. Essentially | |
376 // we need to copy the signature (and in the future, other data like | |
377 // annotations) any time we copy a method as part of our metaprogramming. | |
378 // It might be more friendly to JS metaprogramming if we include this info | |
379 // on the function. | |
380 let originalSigFn = getOwnPropertyDescriptor(type, _methodSig).get; | |
381 defineMemoizedGetter(type, _methodSig, function() { | |
382 let sig = originalSigFn(); | |
383 for (let name of methodNames) { | |
384 sig[getExtensionSymbol(name)] = sig[name]; | |
385 } | |
386 return sig; | |
387 }); | |
388 } | |
389 exports.defineExtensionMembers = defineExtensionMembers; | |
390 | |
391 function canonicalMember(obj, name) { | |
392 if (obj != null && obj[_extensionType]) return dartx[name]; | |
393 // Check for certain names that we can't use in JS | |
394 if (name == 'constructor' || name == 'prototype') { | |
395 name = '+' + name; | |
396 } | |
397 return name; | |
398 } | |
399 exports.canonicalMember = canonicalMember; | |
400 | |
401 /** Sets the type of `obj` to be `type` */ | |
402 function setType(obj, type) { | |
403 obj.__proto__ = type.prototype; | |
404 return obj; | |
405 } | |
406 | |
407 /** Sets the element type of a list literal. */ | |
408 function list(obj, elementType) { | |
409 return setType(obj, _interceptors.JSArray$(elementType)); | |
410 } | |
411 exports.list = list; | |
412 | |
413 function setBaseClass(derived, base) { | |
414 // Link the extension to the type it's extending as a base class. | |
415 derived.prototype.__proto__ = base.prototype; | |
416 } | |
417 exports.setBaseClass = setBaseClass; | |
418 | |
419 }); | |
OLD | NEW |