| 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 association between runtime objects and | |
| 6 * runtime types. | |
| 7 */ | |
| 8 | |
| 9 dart_library.library('dart_runtime/_rtti', null, /* Imports */[ | |
| 10 ], /* Lazy Imports */[ | |
| 11 'dart/core', | |
| 12 'dart_runtime/_types' | |
| 13 ], function(exports, core, types) { | |
| 14 'use strict'; | |
| 15 | |
| 16 const defineLazyProperty = dart_utils.defineLazyProperty; | |
| 17 | |
| 18 const defineProperty = Object.defineProperty; | |
| 19 | |
| 20 const slice = [].slice; | |
| 21 | |
| 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 /** | |
| 55 *Tag a closure with a type, using one of three forms: | |
| 56 * dart.fn(cls) marks cls has having no optional or named | |
| 57 * parameters, with all argument and return types as dynamic | |
| 58 * dart.fn(cls, func) marks cls with the lazily computed | |
| 59 * runtime type as computed by func() | |
| 60 * dart.fn(cls, rType, argsT, extras) marks cls as having the | |
| 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. | |
| 65 */ | |
| 66 function fn(closure/* ...args*/) { | |
| 67 // Closure and a lazy type constructor | |
| 68 if (arguments.length == 2) { | |
| 69 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); | |
| 70 return closure; | |
| 71 } | |
| 72 let t; | |
| 73 if (arguments.length == 1) { | |
| 74 // No type arguments, it's all dynamic | |
| 75 let len = closure.length; | |
| 76 let args = Array.apply(null, new Array(len)).map(() => types.dynamic); | |
| 77 t = types.definiteFunctionType(types.dynamic, args); | |
| 78 } else { | |
| 79 // We're passed the piecewise components of the function type, | |
| 80 // construct it. | |
| 81 let args = slice.call(arguments, 1); | |
| 82 t = types.definiteFunctionType.apply(null, args); | |
| 83 } | |
| 84 tag(closure, t); | |
| 85 return closure; | |
| 86 } | |
| 87 exports.fn = fn; | |
| 88 | |
| 89 // TODO(vsm): How should we encode the runtime type? | |
| 90 const _runtimeType = Symbol('_runtimeType'); | |
| 91 | |
| 92 function checkPrimitiveType(obj) { | |
| 93 switch (typeof obj) { | |
| 94 case "undefined": | |
| 95 return core.Null; | |
| 96 case "number": | |
| 97 return Math.floor(obj) == obj ? core.int : core.double; | |
| 98 case "boolean": | |
| 99 return core.bool; | |
| 100 case "string": | |
| 101 return core.String; | |
| 102 case "symbol": | |
| 103 return Symbol; | |
| 104 } | |
| 105 // Undefined is handled above. For historical reasons, | |
| 106 // typeof null == "object" in JS. | |
| 107 if (obj === null) return core.Null; | |
| 108 return null; | |
| 109 } | |
| 110 | |
| 111 function runtimeType(obj) { | |
| 112 let result = checkPrimitiveType(obj); | |
| 113 if (result !== null) return result; | |
| 114 return obj.runtimeType; | |
| 115 } | |
| 116 exports.runtimeType = runtimeType; | |
| 117 | |
| 118 function getFunctionType(obj) { | |
| 119 // TODO(vsm): Encode this properly on the function for Dart-generated code. | |
| 120 let args = | |
| 121 Array.apply(null, new Array(obj.length)).map(() => types.dynamic); | |
| 122 return types.definiteFunctionType(types.bottom, args); | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` | |
| 127 * but will not call an overridden getter. | |
| 128 * | |
| 129 * Currently this will return null for non-Dart objects. | |
| 130 */ | |
| 131 function realRuntimeType(obj) { | |
| 132 let result = checkPrimitiveType(obj); | |
| 133 if (result !== null) return result; | |
| 134 // TODO(vsm): Should we treat Dart and JS objects differently here? | |
| 135 // E.g., we can check if obj instanceof core.Object to differentiate. | |
| 136 result = obj[_runtimeType]; | |
| 137 if (result) return result; | |
| 138 result = obj.constructor; | |
| 139 if (result == Function) { | |
| 140 // An undecorated Function should have come from | |
| 141 // JavaScript. Treat as untyped. | |
| 142 return types.jsobject; | |
| 143 } | |
| 144 return result; | |
| 145 } | |
| 146 exports.realRuntimeType = realRuntimeType; | |
| 147 | |
| 148 function LazyTagged(infoFn) { | |
| 149 class _Tagged { | |
| 150 get [_runtimeType]() {return infoFn();} | |
| 151 } | |
| 152 return _Tagged; | |
| 153 } | |
| 154 exports.LazyTagged = LazyTagged; | |
| 155 | |
| 156 function read(value) { | |
| 157 return value[_runtimeType]; | |
| 158 } | |
| 159 exports.read = read; | |
| 160 | |
| 161 function tag(value, info) { | |
| 162 value[_runtimeType] = info; | |
| 163 } | |
| 164 exports.tag = tag; | |
| 165 | |
| 166 function tagComputed(value, compute) { | |
| 167 defineProperty(value, _runtimeType, { get: compute }); | |
| 168 } | |
| 169 exports.tagComputed = tagComputed; | |
| 170 | |
| 171 function tagMemoized(value, compute) { | |
| 172 let cache = null; | |
| 173 function getter() { | |
| 174 if (compute == null) return cache; | |
| 175 cache = compute(); | |
| 176 compute = null; | |
| 177 return cache; | |
| 178 } | |
| 179 tagComputed(value, getter); | |
| 180 } | |
| 181 exports.tagMemoized = tagMemoized; | |
| 182 }); | |
| OLD | NEW |