OLD | NEW |
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 association between runtime objects and | |
6 * runtime types. | |
7 */ | |
8 | |
9 dart_library.library('dart/_rtti', null, /* Imports */[ | 1 dart_library.library('dart/_rtti', null, /* Imports */[ |
10 ], /* Lazy Imports */[ | 2 'dart/_utils' |
11 'dart/_utils', | 3 ], /* Lazy imports */[ |
12 'dart/core', | 4 'dart/_types', |
13 'dart/_types' | 5 'dart/core' |
14 ], function(exports, dart_utils, core, types) { | 6 ], function(exports, utils, _types, core) { |
15 'use strict'; | 7 'use strict'; |
16 | 8 const defineLazyProperty = utils.defineLazyProperty; |
17 const defineLazyProperty = dart_utils.defineLazyProperty; | |
18 | |
19 const defineProperty = Object.defineProperty; | 9 const defineProperty = Object.defineProperty; |
20 | |
21 /** | |
22 * Runtime type information. This module defines the mapping from | |
23 * runtime objects to their runtime type information. See the types | |
24 * module for the definition of how type information is represented. | |
25 * | |
26 * Runtime objects fall into four main categories: | |
27 * | |
28 * - Things represented by javascript primitives, such as | |
29 * null, numbers, booleans, strings, and symbols. For these | |
30 * we map directly from the javascript type (given by typeof) | |
31 * to the appropriate class type from core, which serves as their | |
32 * rtti. | |
33 * | |
34 * - Functions, which are represented by javascript functions. | |
35 * Representations of Dart functions always have a | |
36 * _runtimeType property attached to them with the appropriate | |
37 * rtti. | |
38 * | |
39 * - Objects (instances) which are represented by instances of | |
40 * javascript (ES6) classes. Their types are given by their | |
41 * classes, and the rtti is accessed by projecting out their | |
42 * constructor field. | |
43 * | |
44 * - Types objects, which are represented as described in the types | |
45 * module. Types always have a _runtimeType property attached to | |
46 * them with the appropriate rtti. The rtti for these is always | |
47 * core.Type. TODO(leafp): consider the possibility that we can | |
48 * reliably recognize type objects and map directly to core.Type | |
49 * rather than attaching this property everywhere. | |
50 * | |
51 */ | |
52 | |
53 /** | |
54 *Tag a closure with a type, using one of three forms: | |
55 * dart.fn(cls) marks cls has having no optional or named | |
56 * parameters, with all argument and return types as dynamic | |
57 * dart.fn(cls, func) marks cls with the lazily computed | |
58 * runtime type as computed by func() | |
59 * dart.fn(cls, rType, argsT, extras) marks cls as having the | |
60 * runtime type dart.functionType(rType, argsT, extras) | |
61 * | |
62 * Note that since we are producing a type for a concrete function, | |
63 * it is sound to use the definite arrow type. | |
64 */ | |
65 function fn(closure, ...args) { | 10 function fn(closure, ...args) { |
66 // Closure and a lazy type constructor | |
67 if (args.length == 1) { | 11 if (args.length == 1) { |
68 defineLazyProperty(closure, _runtimeType, {get : args[0]}); | 12 defineLazyProperty(closure, _runtimeType, {get: args[0]}); |
69 return closure; | 13 return closure; |
70 } | 14 } |
71 let t; | 15 let t; |
72 if (args.length == 0) { | 16 if (args.length == 0) { |
73 // No type arguments, it's all dynamic | 17 t = _types.definiteFunctionType(_types.dynamic, Array(closure.length).fill
(_types.dynamic)); |
74 t = types.definiteFunctionType( | |
75 types.dynamic, Array(closure.length).fill(types.dynamic)); | |
76 } else { | 18 } else { |
77 // We're passed the piecewise components of the function type, | 19 t = _types.definiteFunctionType.apply(null, args); |
78 // construct it. | |
79 t = types.definiteFunctionType.apply(null, args); | |
80 } | 20 } |
81 tag(closure, t); | 21 tag(closure, t); |
82 return closure; | 22 return closure; |
83 } | 23 } |
84 exports.fn = fn; | 24 const _runtimeType = Symbol("_runtimeType"); |
85 | |
86 // TODO(vsm): How should we encode the runtime type? | |
87 const _runtimeType = Symbol('_runtimeType'); | |
88 | |
89 function checkPrimitiveType(obj) { | 25 function checkPrimitiveType(obj) { |
90 switch (typeof obj) { | 26 switch (typeof obj) { |
91 case "undefined": | 27 case "undefined": |
| 28 { |
92 return core.Null; | 29 return core.Null; |
| 30 } |
93 case "number": | 31 case "number": |
| 32 { |
94 return Math.floor(obj) == obj ? core.int : core.double; | 33 return Math.floor(obj) == obj ? core.int : core.double; |
| 34 } |
95 case "boolean": | 35 case "boolean": |
| 36 { |
96 return core.bool; | 37 return core.bool; |
| 38 } |
97 case "string": | 39 case "string": |
| 40 { |
98 return core.String; | 41 return core.String; |
| 42 } |
99 case "symbol": | 43 case "symbol": |
| 44 { |
100 return Symbol; | 45 return Symbol; |
| 46 } |
101 } | 47 } |
102 // Undefined is handled above. For historical reasons, | |
103 // typeof null == "object" in JS. | |
104 if (obj === null) return core.Null; | 48 if (obj === null) return core.Null; |
105 return null; | 49 return null; |
106 } | 50 } |
107 | |
108 function runtimeType(obj) { | 51 function runtimeType(obj) { |
109 let result = checkPrimitiveType(obj); | 52 let result = checkPrimitiveType(obj); |
110 if (result !== null) return result; | 53 if (result !== null) return result; |
111 return obj.runtimeType; | 54 return obj.runtimeType; |
112 } | 55 } |
113 exports.runtimeType = runtimeType; | |
114 | |
115 function getFunctionType(obj) { | 56 function getFunctionType(obj) { |
116 // TODO(vsm): Encode this properly on the function for Dart-generated code. | 57 let args = Array(obj.length).fill(_types.dynamic); |
117 let args = Array(obj.length).fill(types.dynamic); | 58 return _types.definiteFunctionType(_types.bottom, args); |
118 return types.definiteFunctionType(types.bottom, args); | |
119 } | 59 } |
120 | |
121 /** | |
122 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` | |
123 * but will not call an overridden getter. | |
124 * | |
125 * Currently this will return null for non-Dart objects. | |
126 */ | |
127 function realRuntimeType(obj) { | 60 function realRuntimeType(obj) { |
128 let result = checkPrimitiveType(obj); | 61 let result = checkPrimitiveType(obj); |
129 if (result !== null) return result; | 62 if (result !== null) return result; |
130 // TODO(vsm): Should we treat Dart and JS objects differently here? | |
131 // E.g., we can check if obj instanceof core.Object to differentiate. | |
132 result = obj[_runtimeType]; | 63 result = obj[_runtimeType]; |
133 if (result) return result; | 64 if (result) return result; |
134 result = obj.constructor; | 65 result = obj.constructor; |
135 if (result == Function) { | 66 if (result == Function) { |
136 // An undecorated Function should have come from | 67 return _types.jsobject; |
137 // JavaScript. Treat as untyped. | |
138 return types.jsobject; | |
139 } | 68 } |
140 return result; | 69 return result; |
141 } | 70 } |
142 exports.realRuntimeType = realRuntimeType; | |
143 | |
144 function LazyTagged(infoFn) { | 71 function LazyTagged(infoFn) { |
145 class _Tagged { | 72 class _Tagged { |
146 get [_runtimeType]() {return infoFn();} | 73 get [_runtimeType]() { |
| 74 return infoFn(); |
| 75 } |
147 } | 76 } |
148 return _Tagged; | 77 return _Tagged; |
149 } | 78 } |
150 exports.LazyTagged = LazyTagged; | |
151 | |
152 function read(value) { | 79 function read(value) { |
153 return value[_runtimeType]; | 80 return value[_runtimeType]; |
154 } | 81 } |
155 exports.read = read; | |
156 | |
157 function tag(value, info) { | 82 function tag(value, info) { |
158 value[_runtimeType] = info; | 83 value[_runtimeType] = info; |
159 } | 84 } |
160 exports.tag = tag; | |
161 | |
162 function tagComputed(value, compute) { | 85 function tagComputed(value, compute) { |
163 defineProperty(value, _runtimeType, { get: compute }); | 86 defineProperty(value, _runtimeType, {get: compute}); |
164 } | 87 } |
165 exports.tagComputed = tagComputed; | |
166 | |
167 function tagMemoized(value, compute) { | 88 function tagMemoized(value, compute) { |
168 let cache = null; | 89 let cache = null; |
169 function getter() { | 90 function getter() { |
170 if (compute == null) return cache; | 91 if (compute == null) return cache; |
171 cache = compute(); | 92 cache = compute(); |
172 compute = null; | 93 compute = null; |
173 return cache; | 94 return cache; |
174 } | 95 } |
175 tagComputed(value, getter); | 96 tagComputed(value, getter); |
176 } | 97 } |
| 98 // Exports: |
| 99 exports.defineLazyProperty = defineLazyProperty; |
| 100 exports.defineProperty = defineProperty; |
| 101 exports.fn = fn; |
| 102 exports.checkPrimitiveType = checkPrimitiveType; |
| 103 exports.runtimeType = runtimeType; |
| 104 exports.getFunctionType = getFunctionType; |
| 105 exports.realRuntimeType = realRuntimeType; |
| 106 exports.LazyTagged = LazyTagged; |
| 107 exports.read = read; |
| 108 exports.tag = tag; |
| 109 exports.tagComputed = tagComputed; |
177 exports.tagMemoized = tagMemoized; | 110 exports.tagMemoized = tagMemoized; |
178 }); | 111 }); |
OLD | NEW |