| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Useful compiler idioms. |
| 17 |
* |
| 18 |
* @author [email protected] (John Lenz) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.reflect'); |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Syntax for object literal casts. |
| 26 |
* @see http://go/jscompiler-renaming |
| 27 |
* @see https://goo.gl/CRs09P |
| 28 |
* |
| 29 |
* Use this if you have an object literal whose keys need to have the same names |
| 30 |
* as the properties of some class even after they are renamed by the compiler. |
| 31 |
* |
| 32 |
* @param {!Function} type Type to cast to. |
| 33 |
* @param {Object} object Object literal to cast. |
| 34 |
* @return {Object} The object literal. |
| 35 |
*/ |
| 36 |
goog.reflect.object = function(type, object) { |
| 37 |
return object; |
| 38 |
}; |
| 39 |
|
| 40 |
/** |
| 41 |
* Syntax for renaming property strings. |
| 42 |
* @see http://go/jscompiler-renaming |
| 43 |
* @see https://goo.gl/CRs09P |
| 44 |
* |
| 45 |
* Use this if you have an need to access a property as a string, but want |
| 46 |
* to also have the property renamed by the compiler. In contrast to |
| 47 |
* goog.reflect.object, this method takes an instance of an object. |
| 48 |
* |
| 49 |
* Properties must be simple names (not qualified names). |
| 50 |
* |
| 51 |
* @param {string} prop Name of the property |
| 52 |
* @param {!Object} object Instance of the object whose type will be used |
| 53 |
* for renaming |
| 54 |
* @return {string} The renamed property. |
| 55 |
*/ |
| 56 |
goog.reflect.objectProperty = function(prop, object) { |
| 57 |
return prop; |
| 58 |
}; |
| 59 |
|
| 60 |
/** |
| 61 |
* To assert to the compiler that an operation is needed when it would |
| 62 |
* otherwise be stripped. For example: |
| 63 |
* <code> |
| 64 |
* // Force a layout |
| 65 |
* goog.reflect.sinkValue(dialog.offsetHeight); |
| 66 |
* </code> |
| 67 |
* @param {T} x |
| 68 |
* @return {T} |
| 69 |
* @template T |
| 70 |
*/ |
| 71 |
goog.reflect.sinkValue = function(x) { |
| 72 |
goog.reflect.sinkValue[' '](x); |
| 73 |
return x; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* The compiler should optimize this function away iff no one ever uses |
| 79 |
* goog.reflect.sinkValue. |
| 80 |
*/ |
| 81 |
goog.reflect.sinkValue[' '] = goog.nullFunction; |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* Check if a property can be accessed without throwing an exception. |
| 86 |
* @param {Object} obj The owner of the property. |
| 87 |
* @param {string} prop The property name. |
| 88 |
* @return {boolean} Whether the property is accessible. Will also return true |
| 89 |
* if obj is null. |
| 90 |
*/ |
| 91 |
goog.reflect.canAccessProperty = function(obj, prop) { |
| 92 |
|
| 93 |
try { |
| 94 |
goog.reflect.sinkValue(obj[prop]); |
| 95 |
return true; |
| 96 |
} catch (e) { |
| 97 |
} |
| 98 |
return false; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Retrieves a value from a cache given a key. The compiler provides special |
| 104 |
* consideration for this call such that it is generally considered side-effect |
| 105 |
* free. However, if the {@code opt_keyFn} or {@code valueFn} have side-effects |
| 106 |
* then the entire call is considered to have side-effects. |
| 107 |
* |
| 108 |
* Conventionally storing the value on the cache would be considered a |
| 109 |
* side-effect and preclude unused calls from being pruned, ie. even if |
| 110 |
* the value was never used, it would still always be stored in the cache. |
| 111 |
* |
| 112 |
* Providing a side-effect free {@code valueFn} and {@code opt_keyFn} |
| 113 |
* allows unused calls to {@code goog.reflect.cache} to be pruned. |
| 114 |
* |
| 115 |
* @param {!Object<K, V>} cacheObj The object that contains the cached values. |
| 116 |
* @param {?} key The key to lookup in the cache. If it is not string or number |
| 117 |
* then a {@code opt_keyFn} should be provided. The key is also used as the |
| 118 |
* parameter to the {@code valueFn}. |
| 119 |
* @param {function(?):V} valueFn The value provider to use to calculate the |
| 120 |
* value to store in the cache. This function should be side-effect free |
| 121 |
* to take advantage of the optimization. |
| 122 |
* @param {function(?):K=} opt_keyFn The key provider to determine the cache |
| 123 |
* map key. This should be used if the given key is not a string or number. |
| 124 |
* If not provided then the given key is used. This function should be |
| 125 |
* side-effect free to take advantage of the optimization. |
| 126 |
* @return {V} The cached or calculated value. |
| 127 |
* @template K |
| 128 |
* @template V |
| 129 |
*/ |
| 130 |
goog.reflect.cache = function(cacheObj, key, valueFn, opt_keyFn) { |
| 131 |
var storedKey = opt_keyFn ? opt_keyFn(key) : key; |
| 132 |
|
| 133 |
if (Object.prototype.hasOwnProperty.call(cacheObj, storedKey)) { |
| 134 |
return cacheObj[storedKey]; |
| 135 |
} |
| 136 |
|
| 137 |
return (cacheObj[storedKey] = valueFn(key)); |
| 138 |
}; |