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