optimistic_recompilation.js revision 847:d5c2bf69f341
1279377Simp/*
2279377Simp * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3279377Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4279377Simp *
5279377Simp * This code is free software; you can redistribute it and/or modify it
6279377Simp * under the terms of the GNU General Public License version 2 only, as
7279377Simp * published by the Free Software Foundation.
8279377Simp *
9279377Simp * This code is distributed in the hope that it will be useful, but WITHOUT
10279377Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11279377Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12279377Simp * version 2 for more details (a copy is included in the LICENSE file that
13279377Simp * accompanied this code).
14279377Simp *
15279377Simp * You should have received a copy of the GNU General Public License version
16279377Simp * 2 along with this work; if not, write to the Free Software Foundation,
17279377Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18279377Simp *
19279377Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20279377Simp * or visit www.oracle.com if you need additional information or have any
21279377Simp * questions.
22279377Simp */
23279377Simp
24279377Simp/**
25279377Simp * Ask Debug for an event log of favourable events instead of using --log flags printing to screen
26279377Simp * @test
27279377Simp * @bug 8037086,8038398
28279377Simp * @fork
29279377Simp * @option -Dnashorn.debug=true
30279377Simp * @option --log=recompile:quiet
31279377Simp */
32279377Simp
33279377Simpvar forName       = java.lang.Class["forName(String)"];
34279377Simpvar RuntimeEvent  = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static;
35279377Simpvar getValue      = RuntimeEvent.class.getMethod("getValue");
36279377Simpvar RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static;
37279377Simpvar getReturnType    = RewriteException.class.getMethod("getReturnType");
38279377Simpvar RecompilationEvent = forName("jdk.nashorn.internal.runtime.events.RecompilationEvent").static;
39279377Simpvar getReturnValue     = RecompilationEvent.class.getMethod("getReturnValue");
40279377Simpvar setReturnTypeAndValue = [];
41279377Simpvar expectedValues = [];
42279377Simp
43279377Simpfunction checkExpectedRecompilation(f, expectedValues, testCase) {
44279377Simp    Debug.clearRuntimeEvents();
45279377Simp    print(f());
46279377Simp    events = Debug.getRuntimeEvents();
47279377Simp    //make sure we got runtime events
48279377Simp    print("events = " + (events.toString().indexOf("RuntimeEvent") != -1));
49279377Simp    if (events.length ==  expectedValues.length) {
50279377Simp        for (var i in events) {
51279377Simp            var e = events[i];
52279377Simp            var returnValue = getReturnValue.invoke(e);
53279377Simp            if (typeof returnValue != 'undefined') {
54279377Simp	        setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue];
55279377Simp            } else {
56279377Simp                returnValue = "undefined";
57279377Simp                setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue];
58279377Simp            }
59279377Simp            if (!setReturnTypeAndValue[i].toString().equals(expectedValues[i].toString())) {
60279377Simp                fail("The return values are not as expected. Expected value: " + expectedValues[i] + " and got: " + setReturnTypeAndValue[i] + " in test case: " + f);
61279377Simp            }
62279377Simp        }
63279377Simp    } else {
64279377Simp        fail("Number of Deoptimizing recompilation is not correct, expected: " + expectedValues.length + " and found: " + events.length + " in test case: " + f);
65279377Simp    }
66279377Simp}
67279377Simp
68279377SimpcheckExpectedRecompilation(function divisionByZeroTest() {var x = { a: 2, b:1 }; x.a = Number.POSITIVE_INFINITY; x.b = 0; print(x.a/x.b); return 1;},
69279377Simp                           expectedValues =[['double', 'Infinity']]);
70279377SimpcheckExpectedRecompilation(function divisionWithRemainderTest() {var x = { a: 7, b:2 }; print(x.a/x.b); return 1;}, expectedValues =[['double', '3.5']]);
71279377SimpcheckExpectedRecompilation(function infinityMultiplicationTest() {var x = { a: Number.POSITIVE_INFINITY, b: Number.POSITIVE_INFINITY}; print(x.a*x.b); return 1;},
72279377Simp                           expectedValues =[['double', 'Infinity']]);
73279377SimpcheckExpectedRecompilation(function maxValueMultiplicationTest() {var x = { a: Number.MAX_VALUE, b: Number.MAX_VALUE}; print(x.a*x.b); return 1;},
74279377Simp                           expectedValues =[['double', '1.7976931348623157e+308']]);
75279377SimpcheckExpectedRecompilation(function divisionByInfinityTest() {var x = { a: -1, b: Number.POSITIVE_INFINITY}; print(x.a/x.b); return 1;},
76279377Simp                           expectedValues =[['double', 'Infinity']]);
77279377SimpcheckExpectedRecompilation(function divisionByStringTest() {var x = { a: Number.POSITIVE_INFINITY, b: 'Hello'}; print(x.a/x.b); return 1;},
78279377Simp                           expectedValues =[['double', 'Infinity']]);
79279377SimpcheckExpectedRecompilation(function nestedFunctionTest() {var a=3,b,c; function f() {var x = 2, y =1; function g(){var y = x; var z = a; z = x*y; print(a*b)} g()}f(); return 1;},
80279377Simp                           expectedValues =[['object', 'undefined']]);
81279377SimpcheckExpectedRecompilation(function functionTest(a,b,c) { d = (a + b) * c; print(d); return 1;}, expectedValues =[['double', 'NaN']]);
82279377SimpcheckExpectedRecompilation(function andTest(a,b) { d = a && b; print(d); return 1;}, expectedValues =[['object', 'undefined']]);
83279377Simp