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 
  36 print(Debug);
  37 print();
  38 
  39 var Reflector     = Java.type("jdk.nashorn.test.models.Reflector");
  40 var forName       = java.lang.Class["forName(String)"];
  41 var RuntimeEvent  = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static;
  42 var getValue      = RuntimeEvent.class.getMethod("getValue");
  43 var getValueClass = RuntimeEvent.class.getMethod("getValueClass");
  44 
  45 print(RuntimeEvent);
  46 
  47 var RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static;
  48 var getReturnType    = RewriteException.class.getMethod("getReturnType");
  49 
  50 print(RewriteException);
  51 
  52 var a = [1.1, 2.2];
  53 function f() {
  54     var sum = 2;
  55     for (var i = 0; i < a.length; i++) {
  56     sum *= a[i];
  57     }
  58     return sum;
  59 }
  60 
  61 function 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
  70 Debug.clearRuntimeEvents();
  71 
  72 print();
  73 print(f());
  74 print(g());
  75 
  76 print();
  77 events = Debug.getRuntimeEvents();
  78 print("Done with " + events.length + " in the event queue");
  79 //make sure we got runtime events
  80 print("events = " + (events.toString().indexOf("RuntimeEvent") != -1));
  81 print("events.length = " + events.length);
  82 
  83 var lastInLoop = undefined;
  84 for (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 
  95 print();
  96 print("in loop last class = " + lastInLoop.getClass());
  97 print("in loop last value class = " + Reflector.invoke(getValueClass, lastInLoop));
  98 var rexInLoop = Reflector.invoke(getValue, lastInLoop);
  99 print("in loop rex class = " + rexInLoop.getClass());
 100 print("in loop rex return type = " + Reflector.invoke(getReturnType, rexInLoop));
 101 
 102 //try last runtime events
 103 var last = Debug.getLastRuntimeEvent();
 104 //the code after the loop creates additional rewrite exceptions
 105 print();
 106 print(last !== lastInLoop);
 107 print();
 108 
 109 print("last class = " + last.getClass());
 110 print("last value class = " + Reflector.invoke(getValueClass, last));
 111 var rex = Reflector.invoke(getValue, last);
 112 print("rex class = " + rex.getClass());
 113 print("rex return type = " + Reflector.invoke(getReturnType, rex));
 114 
 115 //try the capacity setter
 116 print();
 117 print(Debug.getEventQueueCapacity());
 118 Debug.setEventQueueCapacity(2048);
 119 print(Debug.getEventQueueCapacity());
 120 
 121 //try clear events
 122 print();
 123 Debug.clearRuntimeEvents();
 124 print(Debug.getRuntimeEvents().length);
 125