Fri, 21 Dec 2012 16:36:24 -0400
8005403: Open-source Nashorn
Reviewed-by: attila, hannesw, lagergren, sundar
Contributed-by: james.laskey@oracle.com, akhil.arora@oracle.com, andreas.woess@jku.at, attila.szegedi@oracle.com, hannes.wallnoefer@oracle.com, henry.jen@oracle.com, marcus.lagergren@oracle.com, pavel.semenov@oracle.com, pavel.stepanov@oracle.com, petr.hejl@oracle.com, petr.pisl@oracle.com, sundararajan.athijegannathan@oracle.com
1 /*
2 * Copyright (c) 2010, 2012, 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.PrintStream;
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.PropertyListenerManager;
37 import jdk.nashorn.internal.runtime.PropertyMap;
38 import jdk.nashorn.internal.runtime.ScriptFunction;
39 import jdk.nashorn.internal.runtime.ScriptObject;
40 import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
42 /**
43 * Nashorn specific debug utils. This is meant for Nashorn developers.
44 * The interface is subject to change without notice!!
45 *
46 */
47 @ScriptClass("Debug")
48 public class NativeDebug extends ScriptObject {
49 NativeDebug() {
50 this.setProto(Global.objectPrototype());
51 }
53 @Override
54 public String getClassName() {
55 return "Debug";
56 }
58 /**
59 * Nashorn extension: get context, context utility
60 *
61 * @param self self reference
62 * @return context
63 */
64 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
65 public static Object getContext(final Object self) {
66 return Global.getThisContext();
67 }
69 /**
70 * Nashorn extension: get map from {@link ScriptObject}
71 *
72 * @param self self reference
73 * @param obj script object
74 * @return the map for the current ScriptObject
75 */
76 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
77 public static Object map(final Object self, final Object obj) {
78 if (obj instanceof ScriptObject) {
79 return ((ScriptObject)obj).getMap();
80 }
81 return UNDEFINED;
82 }
84 /**
85 * Nashorn extension: get embed0 from {@link ScriptObject}
86 *
87 * @param self self reference
88 * @param obj script object
89 * @return the embed0 property value for the given ScriptObject
90 */
91 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
92 public static Object embed0(final Object self, final Object obj) {
93 if (obj instanceof ScriptObject) {
94 return ((ScriptObject)obj).embed0;
95 }
96 return UNDEFINED;
97 }
99 /**
100 * Nashorn extension: get embed1 from {@link ScriptObject}
101 *
102 * @param self self reference
103 * @param obj script object
104 * @return the embed1 property value for the given ScriptObject
105 */
106 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
107 public static Object embed1(final Object self, final Object obj) {
108 if (obj instanceof ScriptObject) {
109 return ((ScriptObject)obj).embed1;
110 }
111 return UNDEFINED;
112 }
114 /**
115 * Nashorn extension: get embed2 from {@link ScriptObject}
116 *
117 * @param self self reference
118 * @param obj script object
119 * @return the embed2 property value for the given ScriptObject
120 */
121 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
122 public static Object embed2(final Object self, final Object obj) {
123 if (obj instanceof ScriptObject) {
124 return ((ScriptObject)obj).embed2;
125 }
126 return UNDEFINED;
127 }
129 /**
130 * Nashorn extension: get embed3 from {@link ScriptObject}
131 *
132 * @param self self reference
133 * @param obj script object
134 * @return the embed3 property value for the given ScriptObject
135 */
136 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
137 public static Object embed3(final Object self, final Object obj) {
138 if (obj instanceof ScriptObject) {
139 return ((ScriptObject)obj).embed3;
140 }
141 return UNDEFINED;
142 }
144 /**
145 * Nashorn extension: get spill vector from {@link ScriptObject}
146 *
147 * @param self self reference
148 * @param obj script object
149 * @return the spill vector for the given ScriptObject
150 */
151 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
152 public static Object spill(final Object self, final Object obj) {
153 if (obj instanceof ScriptObject) {
154 return ((ScriptObject)obj).spill;
155 }
156 return UNDEFINED;
157 }
159 /**
160 * Nashorn extension: get invocation handle from {@link ScriptFunction}
161 *
162 * @param self self reference
163 * @param obj script function
164 * @return the invocation handle for the given ScriptFunction
165 */
166 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
167 public static Object methodHandle(final Object self, final Object obj) {
168 if (obj instanceof ScriptFunction) {
169 return ((ScriptFunction)obj).getInvokeHandle();
170 }
171 return UNDEFINED;
172 }
174 /**
175 * Check object identity comparison regardless of type
176 *
177 * @param self self reference
178 * @param obj1 first object in comparison
179 * @param obj2 second object in comparison
180 * @return true if reference identity
181 */
182 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
183 public static Object identical(final Object self, final Object obj1, final Object obj2) {
184 return obj1 == obj2;
185 }
187 /**
188 * Object util - getClass
189 *
190 * @param self self reference
191 * @param obj object
192 * @return class of {@code obj}
193 */
194 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
195 public static Object getClass(final Object self, final Object obj) {
196 if (obj != null) {
197 return obj.getClass();
198 }
199 return UNDEFINED;
200 }
202 /**
203 * Object util - equals
204 *
205 * @param self self reference
206 * @param obj1 first object in comparison
207 * @param obj2 second object in comparison
208 * @return return {@link Object#equals(Object)} for objects.
209 */
210 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
211 public static Object equals(final Object self, final Object obj1, final Object obj2) {
212 return (obj1 != null) ? obj1.equals(obj2) : false;
213 }
215 /**
216 * Object util - toJavaString
217 *
218 * @param self self reference
219 * @param obj object to represent as a string
220 * @return Java string representation of {@code obj}
221 */
222 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
223 public static Object toJavaString(final Object self, final Object obj) {
224 return Objects.toString(obj);
225 }
227 /**
228 * Do not call overridden toString -- use default toString impl
229 *
230 * @param self self reference
231 * @param obj object to represent as a string
232 * @return string representation
233 */
234 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
235 public static Object toIdentString(final Object self, final Object obj) {
236 if (obj == null) {
237 return "null";
238 }
240 final int hash = System.identityHashCode(obj);
241 return obj.getClass() + "@" + Integer.toHexString(hash);
242 }
244 /**
245 * Dump all Nashorn debug mode counters. Calling this may be better if
246 * you want to print all counters. This way you can avoid too many callsites
247 * due to counter access itself!!
248 * @param self self reference
249 * @return undefined
250 */
251 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
252 public static Object dumpCounters(final Object self) {
253 final PrintStream out = System.err;
254 out.println("ScriptObject count " + ScriptObject.getCount());
255 out.println("Scope count " + ScriptObject.getScopeCount());
256 out.println("ScriptObject listeners added " + PropertyListenerManager.getListenersAdded());
257 out.println("ScriptObject listeners removed " + PropertyListenerManager.getListenersRemoved());
258 out.println("ScriptObject listeners dead " + PropertyListenerManager.getListenersDead());
259 out.println("ScriptFunction count " + ScriptObject.getCount());
260 out.println("ScriptFunction invokes " + ScriptFunction.getInvokes());
261 out.println("ScriptFunction allocations " + ScriptFunction.getAllocations());
262 out.println("PropertyMap count " + PropertyMap.getCount());
263 out.println("PropertyMap cloned " + PropertyMap.getClonedCount());
264 out.println("PropertyMap history hit " + PropertyMap.getHistoryHit());
265 out.println("PropertyMap proto invalidations " + PropertyMap.getProtoInvalidations());
266 out.println("PropertyMap proto history hit " + PropertyMap.getProtoHistoryHit());
267 out.println("PropertyMap setProtoNewMapCount " + PropertyMap.getSetProtoNewMapCount());
268 out.println("Callsite count " + LinkerCallSite.getCount());
269 out.println("Callsite misses " + LinkerCallSite.getMissCount());
270 out.println("Callsite misses by site at " + LinkerCallSite.getMissSamplingPercentage() + "%");
271 LinkerCallSite.getMissCounts(out);
272 return UNDEFINED;
273 }
274 }