Tue, 30 Apr 2013 10:05:42 -0300
8006220: Simplify PropertyMaps
Reviewed-by: hannesw, lagergren
Contributed-by: james.laskey@oracle.com
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package jdk.nashorn.internal.objects;
28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
30 import java.io.PrintWriter;
31 import java.util.Objects;
32 import jdk.nashorn.internal.objects.annotations.Attribute;
33 import jdk.nashorn.internal.objects.annotations.Function;
34 import jdk.nashorn.internal.objects.annotations.ScriptClass;
35 import jdk.nashorn.internal.objects.annotations.Where;
36 import jdk.nashorn.internal.runtime.Context;
37 import jdk.nashorn.internal.runtime.PropertyListenerManager;
38 import jdk.nashorn.internal.runtime.PropertyMap;
39 import jdk.nashorn.internal.runtime.ScriptFunction;
40 import jdk.nashorn.internal.runtime.ScriptObject;
41 import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
43 /**
44 * Nashorn specific debug utils. This is meant for Nashorn developers.
45 * The interface is subject to change without notice!!
46 *
47 */
48 @ScriptClass("Debug")
49 public final class NativeDebug extends ScriptObject {
50 NativeDebug() {
51 this.setProto(Global.objectPrototype());
52 }
54 @Override
55 public String getClassName() {
56 return "Debug";
57 }
59 /**
60 * Nashorn extension: get context, context utility
61 *
62 * @param self self reference
63 * @return context
64 */
65 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
66 public static Object getContext(final Object self) {
67 final SecurityManager sm = System.getSecurityManager();
68 if (sm != null) {
69 sm.checkPermission(new RuntimePermission("nashorn.getContext"));
70 }
71 return Global.getThisContext();
72 }
74 /**
75 * Nashorn extension: get map from {@link ScriptObject}
76 *
77 * @param self self reference
78 * @param obj script object
79 * @return the map for the current ScriptObject
80 */
81 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
82 public static Object map(final Object self, final Object obj) {
83 if (obj instanceof ScriptObject) {
84 return ((ScriptObject)obj).getMap();
85 }
86 return UNDEFINED;
87 }
89 /**
90 * Nashorn extension: get spill vector from {@link ScriptObject}
91 *
92 * @param self self reference
93 * @param obj script object
94 * @return the spill vector for the given ScriptObject
95 */
96 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
97 public static Object spill(final Object self, final Object obj) {
98 if (obj instanceof ScriptObject) {
99 return ((ScriptObject)obj).spill;
100 }
101 return UNDEFINED;
102 }
104 /**
105 * Check object identity comparison regardless of type
106 *
107 * @param self self reference
108 * @param obj1 first object in comparison
109 * @param obj2 second object in comparison
110 * @return true if reference identity
111 */
112 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
113 public static Object identical(final Object self, final Object obj1, final Object obj2) {
114 return obj1 == obj2;
115 }
117 /**
118 * Object util - getClass
119 *
120 * @param self self reference
121 * @param obj object
122 * @return class of {@code obj}
123 */
124 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
125 public static Object getClass(final Object self, final Object obj) {
126 if (obj != null) {
127 return obj.getClass();
128 }
129 return UNDEFINED;
130 }
132 /**
133 * Object util - equals
134 *
135 * @param self self reference
136 * @param obj1 first object in comparison
137 * @param obj2 second object in comparison
138 * @return return {@link Object#equals(Object)} for objects.
139 */
140 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
141 public static Object equals(final Object self, final Object obj1, final Object obj2) {
142 return (obj1 != null) ? obj1.equals(obj2) : false;
143 }
145 /**
146 * Object util - toJavaString
147 *
148 * @param self self reference
149 * @param obj object to represent as a string
150 * @return Java string representation of {@code obj}
151 */
152 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
153 public static Object toJavaString(final Object self, final Object obj) {
154 return Objects.toString(obj);
155 }
157 /**
158 * Do not call overridden toString -- use default toString impl
159 *
160 * @param self self reference
161 * @param obj object to represent as a string
162 * @return string representation
163 */
164 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
165 public static Object toIdentString(final Object self, final Object obj) {
166 if (obj == null) {
167 return "null";
168 }
170 final int hash = System.identityHashCode(obj);
171 return obj.getClass() + "@" + Integer.toHexString(hash);
172 }
174 /**
175 * Dump all Nashorn debug mode counters. Calling this may be better if
176 * you want to print all counters. This way you can avoid too many callsites
177 * due to counter access itself!!
178 * @param self self reference
179 * @return undefined
180 */
181 @SuppressWarnings("resource")
182 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
183 public static Object dumpCounters(final Object self) {
184 final PrintWriter out = Context.getCurrentErr();
186 out.println("ScriptObject count " + ScriptObject.getCount());
187 out.println("Scope count " + ScriptObject.getScopeCount());
188 out.println("ScriptObject listeners added " + PropertyListenerManager.getListenersAdded());
189 out.println("ScriptObject listeners removed " + PropertyListenerManager.getListenersRemoved());
190 out.println("ScriptFunction count " + ScriptObject.getCount());
191 out.println("ScriptFunction invokes " + ScriptFunction.getInvokes());
192 out.println("ScriptFunction allocations " + ScriptFunction.getAllocations());
193 out.println("PropertyMap count " + PropertyMap.getCount());
194 out.println("PropertyMap cloned " + PropertyMap.getClonedCount());
195 out.println("PropertyMap history hit " + PropertyMap.getHistoryHit());
196 out.println("PropertyMap proto invalidations " + PropertyMap.getProtoInvalidations());
197 out.println("PropertyMap proto history hit " + PropertyMap.getProtoHistoryHit());
198 out.println("PropertyMap setProtoNewMapCount " + PropertyMap.getSetProtoNewMapCount());
199 out.println("Callsite count " + LinkerCallSite.getCount());
200 out.println("Callsite misses " + LinkerCallSite.getMissCount());
201 out.println("Callsite misses by site at " + LinkerCallSite.getMissSamplingPercentage() + "%");
203 LinkerCallSite.getMissCounts(out);
205 return UNDEFINED;
206 }
207 }