Chromium Code Reviews| 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 loader.library('dart/rtti', null, /* Imports */[ | |
| 10 ], /* Lazy Imports */[ | |
| 11 'dart/core', | |
| 12 'dart/types' | |
| 13 ], function(exports, core, types) { | |
| 14 'use strict'; | |
| 15 | |
| 16 const defineLazyProperty = js_utils.defineLazyProperty; | |
| 17 const defineProperty = js_utils.defineProperty; | |
| 18 const slice = js_utils.slice; | |
| 19 | |
| 20 /// Tag a closure with a type, using one of three forms: | |
|
Jennifer Messerly
2015/06/12 17:32:25
fyi .. very minor ... I've been trying to use /**
Leaf
2015/06/12 19:52:14
Done.
| |
| 21 /// dart.fn(cls) marks cls has having no optional or named | |
| 22 /// parameters, with all argument and return types as dynamic | |
| 23 /// dart.fn(cls, func) marks cls with the lazily computed | |
| 24 /// runtime type as computed by func() | |
| 25 /// dart.fn(cls, rType, argsT, extras) marks cls as having the | |
| 26 /// runtime type dart.functionType(rType, argsT, extras) | |
| 27 function fn(closure/* ...args*/) { | |
| 28 // Closure and a lazy type constructor | |
| 29 if (arguments.length == 2) { | |
| 30 defineLazyProperty(closure, _runtimeType, {get : arguments[1]}); | |
| 31 return closure; | |
| 32 } | |
| 33 let t; | |
| 34 if (arguments.length == 1) { | |
| 35 // No type arguments, it's all dynamic | |
| 36 let len = closure.length; | |
| 37 let build = () => { | |
| 38 let args = Array.apply(null, new Array(len)).map(() => core.Object); | |
| 39 return types.functionType(core.Object, args); | |
| 40 }; | |
| 41 // We could be called before Object is defined. | |
| 42 if (core.Object === void 0) return fn(closure, build); | |
| 43 t = build(); | |
| 44 } else { | |
| 45 // We're passed the piecewise components of the function type, | |
| 46 // construct it. | |
| 47 let args = slice.call(arguments, 1); | |
| 48 t = types.functionType.apply(null, args); | |
| 49 } | |
| 50 tag(closure, t); | |
| 51 return closure; | |
| 52 } | |
| 53 exports.fn = fn; | |
| 54 | |
| 55 // TODO(vsm): How should we encode the runtime type? | |
| 56 const _runtimeType = Symbol('_runtimeType'); | |
| 57 | |
| 58 function checkPrimitiveType(obj) { | |
| 59 switch (typeof obj) { | |
| 60 case "undefined": | |
| 61 return core.Null; | |
| 62 case "number": | |
| 63 return Math.floor(obj) == obj ? core.int : core.double; | |
| 64 case "boolean": | |
| 65 return core.bool; | |
| 66 case "string": | |
| 67 return core.String; | |
| 68 case "symbol": | |
| 69 return Symbol; | |
| 70 } | |
| 71 // Undefined is handled above. For historical reasons, | |
| 72 // typeof null == "object" in JS. | |
| 73 if (obj === null) return core.Null; | |
| 74 return null; | |
| 75 } | |
| 76 | |
| 77 function runtimeType(obj) { | |
| 78 let result = checkPrimitiveType(obj); | |
| 79 if (result !== null) return result; | |
| 80 return obj.runtimeType; | |
| 81 } | |
| 82 exports.runtimeType = runtimeType; | |
| 83 | |
| 84 function getFunctionType(obj) { | |
| 85 // TODO(vsm): Encode this properly on the function for Dart-generated code. | |
| 86 let args = Array.apply(null, new Array(obj.length)).map(() => core.Object); | |
| 87 return types.functionType(types.bottom, args); | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` | |
| 92 * but will not call an overridden getter. | |
| 93 * | |
| 94 * Currently this will return null for non-Dart objects. | |
| 95 */ | |
| 96 function realRuntimeType(obj) { | |
| 97 let result = checkPrimitiveType(obj); | |
| 98 if (result !== null) return result; | |
| 99 // TODO(vsm): Should we treat Dart and JS objects differently here? | |
| 100 // E.g., we can check if obj instanceof core.Object to differentiate. | |
| 101 result = obj[_runtimeType]; | |
| 102 if (result) return result; | |
| 103 result = obj.constructor; | |
| 104 if (result == Function) { | |
| 105 return getFunctionType(obj); | |
| 106 } | |
| 107 return result; | |
| 108 } | |
| 109 exports.realRuntimeType = realRuntimeType; | |
| 110 | |
| 111 function LazyTagged(infoFn) { | |
| 112 class _Tagged { | |
| 113 get [_runtimeType]() {return infoFn();} | |
| 114 } | |
| 115 return _Tagged; | |
| 116 } | |
| 117 exports.LazyTagged = LazyTagged; | |
| 118 | |
| 119 function read(value) { | |
| 120 return value[_runtimeType]; | |
| 121 } | |
| 122 exports.read = read; | |
| 123 | |
| 124 function tag(value, info) { | |
| 125 value[_runtimeType] = info; | |
| 126 } | |
| 127 exports.tag = tag; | |
| 128 | |
| 129 function tagComputed(value, compute) { | |
| 130 defineProperty(value, _runtimeType, { get: compute }); | |
| 131 } | |
| 132 exports.tagComputed = tagComputed; | |
| 133 | |
| 134 function tagMemoized(value, compute) { | |
| 135 let cache = null; | |
| 136 function getter() { | |
| 137 if (compute == null) return cache; | |
| 138 cache = compute(); | |
| 139 compute = null; | |
| 140 return cache; | |
| 141 } | |
| 142 tagComputed(value, getter); | |
| 143 } | |
| 144 exports.tagMemoized = tagMemoized; | |
| 145 }); | |
| OLD | NEW |