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 | */ |
19 | |
20 | goog.provide('goog.reflect'); |
21 | |
22 | |
23 | /** |
24 | * Syntax for object literal casts. |
25 | * @see http://go/jscompiler-renaming |
26 | * @see http://code.google.com/p/closure-compiler/wiki/ |
27 | * ExperimentalTypeBasedPropertyRenaming |
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 | /** |
42 | * To assert to the compiler that an operation is needed when it would |
43 | * otherwise be stripped. For example: |
44 | * <code> |
45 | * // Force a layout |
46 | * goog.reflect.sinkValue(dialog.offsetHeight); |
47 | * </code> |
48 | * @type {!Function} |
49 | */ |
50 | goog.reflect.sinkValue = function(x) { |
51 | goog.reflect.sinkValue[' '](x); |
52 | return x; |
53 | }; |
54 | |
55 | |
56 | /** |
57 | * The compiler should optimize this function away iff no one ever uses |
58 | * goog.reflect.sinkValue. |
59 | */ |
60 | goog.reflect.sinkValue[' '] = goog.nullFunction; |
61 | |
62 | |
63 | /** |
64 | * Check if a property can be accessed without throwing an exception. |
65 | * @param {Object} obj The owner of the property. |
66 | * @param {string} prop The property name. |
67 | * @return {boolean} Whether the property is accessible. Will also return true |
68 | * if obj is null. |
69 | */ |
70 | goog.reflect.canAccessProperty = function(obj, prop) { |
71 | /** @preserveTry */ |
72 | try { |
73 | goog.reflect.sinkValue(obj[prop]); |
74 | return true; |
75 | } catch (e) {} |
76 | return false; |
77 | }; |