event_queue.js revision 830:03b2757e2eba
1/*
2 * Copyright (c) 2010, 2014, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * Debug.eventqueue test - instead of screen scraping, test the concept of asking Debug for
26 * an event log of favourable events.
27 *
28 * @test
29 * @fork
30 * @option -Dnashorn.debug=true
31 * @option --log=recompile:quiet
32 */
33
34print(Debug);
35print();
36
37var forName       = java.lang.Class["forName(String)"];
38var RuntimeEvent  = forName("jdk.nashorn.internal.runtime.RuntimeEvent").static;
39var getValue      = RuntimeEvent.class.getMethod("getValue");
40var getValueClass = RuntimeEvent.class.getMethod("getValueClass");
41
42print(RuntimeEvent);
43
44var RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static;
45var getReturnType    = RewriteException.class.getMethod("getReturnType");
46
47print(RewriteException);
48
49var a = [1.1, 2.2];
50function f() {
51    var sum = 2;
52    for (var i = 0; i < a.length; i++) {
53	sum *= a[i];
54    }
55    return sum;
56}
57
58function g() {
59    var diff = 17;
60    for (var i = 0; i < a.length; i++) {
61	diff -= a[i];
62    }
63    return diff;
64}
65
66//kill anything that may already be in the event queue from earlier debug runs
67Debug.clearRuntimeEvents();
68
69print();
70print(f());
71print(g());
72
73print();
74events = Debug.getRuntimeEvents();
75print("Done with " + events.length + " in the event queue");
76//make sure we got runtime events
77print("events = " + (events.toString().indexOf("RuntimeEvent") != -1));
78print("events.length = " + events.length);
79
80var lastInLoop = undefined;
81for (var i = 0; i < events.length; i++) {
82    var e = events[i];
83    print("event #" + i);
84    print("\tevent class=" + e.getClass());
85    print("\tvalueClass in event=" + getValueClass.invoke(e));
86    var v = getValue.invoke(e);
87    print("\tclass of value=" + v.getClass());
88    print("\treturn type=" + getReturnType.invoke(v));
89    lastInLoop = events[i];
90}
91
92print();
93print("in loop last class = " + lastInLoop.getClass());
94print("in loop last value class = " + getValueClass.invoke(lastInLoop));
95var rexInLoop = getValue.invoke(lastInLoop);
96print("in loop rex class = " + rexInLoop.getClass());
97print("in loop rex return type = " + getReturnType.invoke(rexInLoop));
98
99//try last runtime events
100var last = Debug.getLastRuntimeEvent();
101//the code after the loop creates additional rewrite exceptions
102print();
103print(last !== lastInLoop);
104print();
105
106print("last class = " + last.getClass());
107print("last value class = " + getValueClass.invoke(last));
108var rex = getValue.invoke(last);
109print("rex class = " + rex.getClass());
110print("rex return type = " + getReturnType.invoke(rex));
111
112//try the capacity setter
113print();
114print(Debug.getEventQueueCapacity());
115Debug.setEventQueueCapacity(2048);
116print(Debug.getEventQueueCapacity());
117
118//try clear events
119print();
120Debug.clearRuntimeEvents();
121print(Debug.getRuntimeEvents().length);
122
123