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 */ | 7 */ |
8 | 8 |
9 dart_library.library('dart_runtime/_rtti', null, /* Imports */[ | 9 dart_library.library('dart_runtime/_rtti', null, /* Imports */[ |
10 ], /* Lazy Imports */[ | 10 ], /* Lazy Imports */[ |
11 'dart/core', | 11 'dart/core', |
12 'dart_runtime/_types' | 12 'dart_runtime/_types' |
13 ], function(exports, core, types) { | 13 ], function(exports, core, types) { |
14 'use strict'; | 14 'use strict'; |
15 | 15 |
16 const defineLazyProperty = dart_utils.defineLazyProperty; | 16 const defineLazyProperty = dart_utils.defineLazyProperty; |
17 | 17 |
18 const defineProperty = Object.defineProperty; | 18 const defineProperty = Object.defineProperty; |
19 | 19 |
20 const slice = [].slice; | 20 const slice = [].slice; |
21 | 21 |
22 /** | 22 /** |
| 23 * Runtime type information. This module defines the mapping from |
| 24 * runtime objects to their runtime type information. See the types |
| 25 * module for the definition of how type information is represented. |
| 26 * |
| 27 * Runtime objects fall into four main categories: |
| 28 * |
| 29 * - Things represented by javascript primitives, such as |
| 30 * null, numbers, booleans, strings, and symbols. For these |
| 31 * we map directly from the javascript type (given by typeof) |
| 32 * to the appropriate class type from core, which serves as their |
| 33 * rtti. |
| 34 * |
| 35 * - Functions, which are represented by javascript functions. |
| 36 * Representations of Dart functions always have a |
| 37 * _runtimeType property attached to them with the appropriate |
| 38 * rtti. |
| 39 * |
| 40 * - Objects (instances) which are represented by instances of |
| 41 * javascript (ES6) classes. Their types are given by their |
| 42 * classes, and the rtti is accessed by projecting out their |
| 43 * constructor field. |
| 44 * |
| 45 * - Types objects, which are represented as described in the types |
| 46 * module. Types always have a _runtimeType property attached to |
| 47 * them with the appropriate rtti. The rtti for these is always |
| 48 * core.Type. TODO(leafp): consider the possibility that we can |
| 49 * reliably recognize type objects and map directly to core.Type |
| 50 * rather than attaching this property everywhere. |
| 51 * |
| 52 */ |
| 53 |
| 54 /** |
23 *Tag a closure with a type, using one of three forms: | 55 *Tag a closure with a type, using one of three forms: |
24 * dart.fn(cls) marks cls has having no optional or named | 56 * dart.fn(cls) marks cls has having no optional or named |
25 * parameters, with all argument and return types as dynamic | 57 * parameters, with all argument and return types as dynamic |
26 * dart.fn(cls, func) marks cls with the lazily computed | 58 * dart.fn(cls, func) marks cls with the lazily computed |
27 * runtime type as computed by func() | 59 * runtime type as computed by func() |
28 * dart.fn(cls, rType, argsT, extras) marks cls as having the | 60 * dart.fn(cls, rType, argsT, extras) marks cls as having the |
29 * runtime type dart.functionType(rType, argsT, extras) | 61 * runtime type dart.functionType(rType, argsT, extras) |
| 62 * |
| 63 * Note that since we are producing a type for a concrete function, |
| 64 * it is sound to use the definite arrow type. |
30 */ | 65 */ |
31 function fn(closure/* ...args*/) { | 66 function fn(closure/* ...args*/) { |
32 // Closure and a lazy type constructor | 67 // Closure and a lazy type constructor |
33 if (arguments.length == 2) { | 68 if (arguments.length == 2) { |
34 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); | 69 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); |
35 return closure; | 70 return closure; |
36 } | 71 } |
37 let t; | 72 let t; |
38 if (arguments.length == 1) { | 73 if (arguments.length == 1) { |
39 // No type arguments, it's all dynamic | 74 // No type arguments, it's all dynamic |
40 let len = closure.length; | 75 let len = closure.length; |
41 let build = () => { | 76 let args = Array.apply(null, new Array(len)).map(() => types.dynamic); |
42 let args = Array.apply(null, new Array(len)).map(() => core.Object); | 77 t = types.definiteFunctionType(types.dynamic, args); |
43 return types.functionType(core.Object, args); | |
44 }; | |
45 // We could be called before Object is defined. | |
46 if (core.Object === void 0) return fn(closure, build); | |
47 t = build(); | |
48 } else { | 78 } else { |
49 // We're passed the piecewise components of the function type, | 79 // We're passed the piecewise components of the function type, |
50 // construct it. | 80 // construct it. |
51 let args = slice.call(arguments, 1); | 81 let args = slice.call(arguments, 1); |
52 t = types.functionType.apply(null, args); | 82 t = types.definiteFunctionType.apply(null, args); |
53 } | 83 } |
54 tag(closure, t); | 84 tag(closure, t); |
55 return closure; | 85 return closure; |
56 } | 86 } |
57 exports.fn = fn; | 87 exports.fn = fn; |
58 | 88 |
59 // TODO(vsm): How should we encode the runtime type? | 89 // TODO(vsm): How should we encode the runtime type? |
60 const _runtimeType = Symbol('_runtimeType'); | 90 const _runtimeType = Symbol('_runtimeType'); |
61 | 91 |
62 function checkPrimitiveType(obj) { | 92 function checkPrimitiveType(obj) { |
(...skipping 17 matching lines...) Expand all Loading... |
80 | 110 |
81 function runtimeType(obj) { | 111 function runtimeType(obj) { |
82 let result = checkPrimitiveType(obj); | 112 let result = checkPrimitiveType(obj); |
83 if (result !== null) return result; | 113 if (result !== null) return result; |
84 return obj.runtimeType; | 114 return obj.runtimeType; |
85 } | 115 } |
86 exports.runtimeType = runtimeType; | 116 exports.runtimeType = runtimeType; |
87 | 117 |
88 function getFunctionType(obj) { | 118 function getFunctionType(obj) { |
89 // TODO(vsm): Encode this properly on the function for Dart-generated code. | 119 // TODO(vsm): Encode this properly on the function for Dart-generated code. |
90 let args = Array.apply(null, new Array(obj.length)).map(() => core.Object); | 120 let args = |
91 return types.functionType(types.bottom, args); | 121 Array.apply(null, new Array(obj.length)).map(() => types.dynamic); |
| 122 return types.definiteFunctionType(types.bottom, args); |
92 } | 123 } |
93 | 124 |
94 /** | 125 /** |
95 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` | 126 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` |
96 * but will not call an overridden getter. | 127 * but will not call an overridden getter. |
97 * | 128 * |
98 * Currently this will return null for non-Dart objects. | 129 * Currently this will return null for non-Dart objects. |
99 */ | 130 */ |
100 function realRuntimeType(obj) { | 131 function realRuntimeType(obj) { |
101 let result = checkPrimitiveType(obj); | 132 let result = checkPrimitiveType(obj); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 function getter() { | 171 function getter() { |
141 if (compute == null) return cache; | 172 if (compute == null) return cache; |
142 cache = compute(); | 173 cache = compute(); |
143 compute = null; | 174 compute = null; |
144 return cache; | 175 return cache; |
145 } | 176 } |
146 tagComputed(value, getter); | 177 tagComputed(value, getter); |
147 } | 178 } |
148 exports.tagMemoized = tagMemoized; | 179 exports.tagMemoized = tagMemoized; |
149 }); | 180 }); |
OLD | NEW |