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 * @option --optimistic-types=true
33 * @run
34 */
35
36print(Debug);
37print();
38
39var Reflector     = Java.type("jdk.nashorn.test.models.Reflector");
40var forName       = java.lang.Class["forName(String)"];
41var RuntimeEvent  = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static;
42var getValue      = RuntimeEvent.class.getMethod("getValue");
43var getValueClass = RuntimeEvent.class.getMethod("getValueClass");
44
45print(RuntimeEvent);
46
47var RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static;
48var getReturnType    = RewriteException.class.getMethod("getReturnType");
49
50print(RewriteException);
51
52var a = [1.1, 2.2];
53function f() {
54    var sum = 2;
55    for (var i = 0; i < a.length; i++) {
56    sum *= a[i];
57    }
58    return sum;
59}
60
61function g() {
62    var diff = 17;
63    for (var i = 0; i < a.length; i++) {
64    diff -= a[i];
65    }
66    return diff;
67}
68
69//kill anything that may already be in the event queue from earlier debug runs
70Debug.clearRuntimeEvents();
71
72print();
73print(f());
74print(g());
75
76print();
77events = Debug.getRuntimeEvents();
78print("Done with " + events.length + " in the event queue");
79//make sure we got runtime events
80print("events = " + (events.toString().indexOf("RuntimeEvent") != -1));
81print("events.length = " + events.length);
82
83var lastInLoop = undefined;
84for (var i = 0; i < events.length; i++) {
85    var e = events[i];
86    print("event #" + i);
87    print("\tevent class=" + e.getClass());
88    print("\tvalueClass in event=" + Reflector.invoke(getValueClass, e));
89    var v = Reflector.invoke(getValue, e);
90    print("\tclass of value=" + v.getClass());
91    print("\treturn type=" + Reflector.invoke(getReturnType, v));
92    lastInLoop = events[i];
93}
94
95print();
96print("in loop last class = " + lastInLoop.getClass());
97print("in loop last value class = " + Reflector.invoke(getValueClass, lastInLoop));
98var rexInLoop = Reflector.invoke(getValue, lastInLoop);
99print("in loop rex class = " + rexInLoop.getClass());
100print("in loop rex return type = " + Reflector.invoke(getReturnType, rexInLoop));
101
102//try last runtime events
103var last = Debug.getLastRuntimeEvent();
104//the code after the loop creates additional rewrite exceptions
105print();
106print(last !== lastInLoop);
107print();
108
109print("last class = " + last.getClass());
110print("last value class = " + Reflector.invoke(getValueClass, last));
111var rex = Reflector.invoke(getValue, last);
112print("rex class = " + rex.getClass());
113print("rex return type = " + Reflector.invoke(getReturnType, rex));
114
115//try the capacity setter
116print();
117print(Debug.getEventQueueCapacity());
118Debug.setEventQueueCapacity(2048);
119print(Debug.getEventQueueCapacity());
120
121//try clear events
122print();
123Debug.clearRuntimeEvents();
124print(Debug.getRuntimeEvents().length);
125
126