| 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/_rtti', null, /* Imports */[ |
| 10 ], /* Lazy Imports */[ | 10 ], /* Lazy Imports */[ |
| 11 'dart/core', | 11 'dart/core', |
| 12 'dart_runtime/_types' | 12 'dart/_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; | |
| 21 | |
| 22 /** | 20 /** |
| 23 * Runtime type information. This module defines the mapping from | 21 * Runtime type information. This module defines the mapping from |
| 24 * runtime objects to their runtime type information. See the types | 22 * runtime objects to their runtime type information. See the types |
| 25 * module for the definition of how type information is represented. | 23 * module for the definition of how type information is represented. |
| 26 * | 24 * |
| 27 * Runtime objects fall into four main categories: | 25 * Runtime objects fall into four main categories: |
| 28 * | 26 * |
| 29 * - Things represented by javascript primitives, such as | 27 * - Things represented by javascript primitives, such as |
| 30 * null, numbers, booleans, strings, and symbols. For these | 28 * null, numbers, booleans, strings, and symbols. For these |
| 31 * we map directly from the javascript type (given by typeof) | 29 * we map directly from the javascript type (given by typeof) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 56 * dart.fn(cls) marks cls has having no optional or named | 54 * dart.fn(cls) marks cls has having no optional or named |
| 57 * parameters, with all argument and return types as dynamic | 55 * parameters, with all argument and return types as dynamic |
| 58 * dart.fn(cls, func) marks cls with the lazily computed | 56 * dart.fn(cls, func) marks cls with the lazily computed |
| 59 * runtime type as computed by func() | 57 * runtime type as computed by func() |
| 60 * dart.fn(cls, rType, argsT, extras) marks cls as having the | 58 * dart.fn(cls, rType, argsT, extras) marks cls as having the |
| 61 * runtime type dart.functionType(rType, argsT, extras) | 59 * runtime type dart.functionType(rType, argsT, extras) |
| 62 * | 60 * |
| 63 * Note that since we are producing a type for a concrete function, | 61 * Note that since we are producing a type for a concrete function, |
| 64 * it is sound to use the definite arrow type. | 62 * it is sound to use the definite arrow type. |
| 65 */ | 63 */ |
| 66 function fn(closure/* ...args*/) { | 64 function fn(closure, ...args) { |
| 67 // Closure and a lazy type constructor | 65 // Closure and a lazy type constructor |
| 68 if (arguments.length == 2) { | 66 if (args.length == 1) { |
| 69 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); | 67 defineLazyProperty(closure, _runtimeType, {get : args[0]}); |
| 70 return closure; | 68 return closure; |
| 71 } | 69 } |
| 72 let t; | 70 let t; |
| 73 if (arguments.length == 1) { | 71 if (args.length == 0) { |
| 74 // No type arguments, it's all dynamic | 72 // No type arguments, it's all dynamic |
| 75 let len = closure.length; | 73 t = types.definiteFunctionType( |
| 76 let args = Array.apply(null, new Array(len)).map(() => types.dynamic); | 74 types.dynamic, Array(closure.length).fill(types.dynamic)); |
| 77 t = types.definiteFunctionType(types.dynamic, args); | |
| 78 } else { | 75 } else { |
| 79 // We're passed the piecewise components of the function type, | 76 // We're passed the piecewise components of the function type, |
| 80 // construct it. | 77 // construct it. |
| 81 let args = slice.call(arguments, 1); | |
| 82 t = types.definiteFunctionType.apply(null, args); | 78 t = types.definiteFunctionType.apply(null, args); |
| 83 } | 79 } |
| 84 tag(closure, t); | 80 tag(closure, t); |
| 85 return closure; | 81 return closure; |
| 86 } | 82 } |
| 87 exports.fn = fn; | 83 exports.fn = fn; |
| 88 | 84 |
| 89 // TODO(vsm): How should we encode the runtime type? | 85 // TODO(vsm): How should we encode the runtime type? |
| 90 const _runtimeType = Symbol('_runtimeType'); | 86 const _runtimeType = Symbol('_runtimeType'); |
| 91 | 87 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 110 | 106 |
| 111 function runtimeType(obj) { | 107 function runtimeType(obj) { |
| 112 let result = checkPrimitiveType(obj); | 108 let result = checkPrimitiveType(obj); |
| 113 if (result !== null) return result; | 109 if (result !== null) return result; |
| 114 return obj.runtimeType; | 110 return obj.runtimeType; |
| 115 } | 111 } |
| 116 exports.runtimeType = runtimeType; | 112 exports.runtimeType = runtimeType; |
| 117 | 113 |
| 118 function getFunctionType(obj) { | 114 function getFunctionType(obj) { |
| 119 // TODO(vsm): Encode this properly on the function for Dart-generated code. | 115 // TODO(vsm): Encode this properly on the function for Dart-generated code. |
| 120 let args = | 116 let args = Array(obj.length).fill(types.dynamic); |
| 121 Array.apply(null, new Array(obj.length)).map(() => types.dynamic); | |
| 122 return types.definiteFunctionType(types.bottom, args); | 117 return types.definiteFunctionType(types.bottom, args); |
| 123 } | 118 } |
| 124 | 119 |
| 125 /** | 120 /** |
| 126 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` | 121 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` |
| 127 * but will not call an overridden getter. | 122 * but will not call an overridden getter. |
| 128 * | 123 * |
| 129 * Currently this will return null for non-Dart objects. | 124 * Currently this will return null for non-Dart objects. |
| 130 */ | 125 */ |
| 131 function realRuntimeType(obj) { | 126 function realRuntimeType(obj) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 146 exports.realRuntimeType = realRuntimeType; | 141 exports.realRuntimeType = realRuntimeType; |
| 147 | 142 |
| 148 function LazyTagged(infoFn) { | 143 function LazyTagged(infoFn) { |
| 149 class _Tagged { | 144 class _Tagged { |
| 150 get [_runtimeType]() {return infoFn();} | 145 get [_runtimeType]() {return infoFn();} |
| 151 } | 146 } |
| 152 return _Tagged; | 147 return _Tagged; |
| 153 } | 148 } |
| 154 exports.LazyTagged = LazyTagged; | 149 exports.LazyTagged = LazyTagged; |
| 155 | 150 |
| 156 function read(value) { | 151 function read(value) { |
| 157 return value[_runtimeType]; | 152 return value[_runtimeType]; |
| 158 } | 153 } |
| 159 exports.read = read; | 154 exports.read = read; |
| 160 | 155 |
| 161 function tag(value, info) { | 156 function tag(value, info) { |
| 162 value[_runtimeType] = info; | 157 value[_runtimeType] = info; |
| 163 } | 158 } |
| 164 exports.tag = tag; | 159 exports.tag = tag; |
| 165 | 160 |
| 166 function tagComputed(value, compute) { | 161 function tagComputed(value, compute) { |
| 167 defineProperty(value, _runtimeType, { get: compute }); | 162 defineProperty(value, _runtimeType, { get: compute }); |
| 168 } | 163 } |
| 169 exports.tagComputed = tagComputed; | 164 exports.tagComputed = tagComputed; |
| 170 | 165 |
| 171 function tagMemoized(value, compute) { | 166 function tagMemoized(value, compute) { |
| 172 let cache = null; | 167 let cache = null; |
| 173 function getter() { | 168 function getter() { |
| 174 if (compute == null) return cache; | 169 if (compute == null) return cache; |
| 175 cache = compute(); | 170 cache = compute(); |
| 176 compute = null; | 171 compute = null; |
| 177 return cache; | 172 return cache; |
| 178 } | 173 } |
| 179 tagComputed(value, getter); | 174 tagComputed(value, getter); |
| 180 } | 175 } |
| 181 exports.tagMemoized = tagMemoized; | 176 exports.tagMemoized = tagMemoized; |
| 182 }); | 177 }); |
| OLD | NEW |