Path: blob/trunk/third_party/closure/goog/reflect/reflect.js
4136 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Useful compiler idioms.8*/910goog.provide('goog.reflect');111213/**14* Syntax for object literal casts.15* @see http://go/jscompiler-renaming16* @see https://goo.gl/CRs09P17*18* Use this if you have an object literal whose keys need to have the same names19* as the properties of some class even after they are renamed by the compiler.20*21* @param {!Function} type Type to cast to.22* @param {Object} object Object literal to cast.23* @return {Object} The object literal.24*/25goog.reflect.object = function(type, object) {26'use strict';27return object;28};2930/**31* Syntax for renaming property strings.32* @see http://go/jscompiler-renaming33* @see https://goo.gl/CRs09P34*35* Use this if you have an need to access a property as a string, but want36* to also have the property renamed by the compiler. In contrast to37* goog.reflect.object, this method takes an instance of an object.38*39* Properties must be simple names (not qualified names).40*41* @param {string} prop Name of the property42* @param {!Object} object Instance of the object whose type will be used43* for renaming44* @return {string} The renamed property.45*/46goog.reflect.objectProperty = function(prop, object) {47'use strict';48return prop;49};5051/**52* To assert to the compiler that an operation is needed when it would53* otherwise be stripped. For example:54* <code>55* // Force a layout56* goog.reflect.sinkValue(dialog.offsetHeight);57* </code>58* @param {T} x59* @return {T}60* @template T61*/62goog.reflect.sinkValue = function(x) {63'use strict';64goog.reflect.sinkValue[' '](x);65return x;66};676869/**70* The compiler should optimize this function away iff no one ever uses71* goog.reflect.sinkValue.72*/73goog.reflect.sinkValue[' '] = function() {};747576/**77* Check if a property can be accessed without throwing an exception.78* @param {Object} obj The owner of the property.79* @param {string} prop The property name.80* @return {boolean} Whether the property is accessible. Will also return true81* if obj is null.82*/83goog.reflect.canAccessProperty = function(obj, prop) {84'use strict';85try {86goog.reflect.sinkValue(obj[prop]);87return true;88} catch (e) {89}90return false;91};929394/**95* Retrieves a value from a cache given a key. The compiler provides special96* consideration for this call such that it is generally considered side-effect97* free. However, if the `opt_keyFn` or `valueFn` have side-effects98* then the entire call is considered to have side-effects.99*100* Conventionally storing the value on the cache would be considered a101* side-effect and preclude unused calls from being pruned, ie. even if102* the value was never used, it would still always be stored in the cache.103*104* Providing a side-effect free `valueFn` and `opt_keyFn`105* allows unused calls to `goog.reflect.cache` to be pruned.106*107* @param {!Object<K, V>} cacheObj The object that contains the cached values.108* @param {?} key The key to lookup in the cache. If it is not string or number109* then a `opt_keyFn` should be provided. The key is also used as the110* parameter to the `valueFn`.111* @param {function(?):V} valueFn The value provider to use to calculate the112* value to store in the cache. This function should be side-effect free113* to take advantage of the optimization.114* @param {function(?):K=} opt_keyFn The key provider to determine the cache115* map key. This should be used if the given key is not a string or number.116* If not provided then the given key is used. This function should be117* side-effect free to take advantage of the optimization.118* @return {V} The cached or calculated value.119* @template K120* @template V121*/122goog.reflect.cache = function(cacheObj, key, valueFn, opt_keyFn) {123'use strict';124const storedKey = opt_keyFn ? opt_keyFn(key) : key;125126if (Object.prototype.hasOwnProperty.call(cacheObj, storedKey)) {127return cacheObj[storedKey];128}129130return (cacheObj[storedKey] = valueFn(key));131};132133134