attila@963: /* attila@963: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@963: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. attila@963: * attila@963: * This code is free software; you can redistribute it and/or modify it attila@963: * under the terms of the GNU General Public License version 2 only, as attila@963: * published by the Free Software Foundation. attila@963: * attila@963: * This code is distributed in the hope that it will be useful, but WITHOUT attila@963: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or attila@963: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License attila@963: * version 2 for more details (a copy is included in the LICENSE file that attila@963: * accompanied this code). attila@963: * attila@963: * You should have received a copy of the GNU General Public License version attila@963: * 2 along with this work; if not, write to the Free Software Foundation, attila@963: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. attila@963: * attila@963: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA attila@963: * or visit www.oracle.com if you need additional information or have any attila@963: * questions. attila@963: */ attila@963: attila@963: /** attila@963: * Ask Debug for an event log of favourable events instead of using --log flags printing to screen attila@963: * @test attila@963: * @bug 8037086,8038398 attila@963: * @fork attila@963: * @option -Dnashorn.debug=true attila@963: * @option --log=recompile:quiet attila@963: * @option --optimistic-types=true attila@963: */ attila@963: attila@963: var forName = java.lang.Class["forName(String)"]; attila@963: var RuntimeEvent = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static; attila@963: var getValue = RuntimeEvent.class.getMethod("getValue"); attila@963: var RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static; attila@963: var getReturnType = RewriteException.class.getMethod("getReturnType"); attila@963: var RecompilationEvent = forName("jdk.nashorn.internal.runtime.events.RecompilationEvent").static; attila@963: var getReturnValue = RecompilationEvent.class.getMethod("getReturnValue"); attila@963: var setReturnTypeAndValue = []; attila@963: var expectedValues = []; attila@963: attila@963: function checkExpectedRecompilation(f, expectedValues, testCase) { attila@963: Debug.clearRuntimeEvents(); attila@963: print(f()); attila@963: events = Debug.getRuntimeEvents(); attila@963: //make sure we got runtime events attila@963: print("events = " + (events.toString().indexOf("RuntimeEvent") != -1)); attila@963: if (events.length == expectedValues.length) { attila@963: for (var i in events) { attila@963: var e = events[i]; attila@963: var returnValue = getReturnValue.invoke(e); attila@963: if (typeof returnValue != 'undefined') { attila@963: setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue]; attila@963: } else { attila@963: returnValue = "undefined"; attila@963: setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue]; attila@963: } attila@963: if (!setReturnTypeAndValue[i].toString().equals(expectedValues[i].toString())) { attila@963: fail("The return values are not as expected. Expected value: " + expectedValues[i] + " and got: " + setReturnTypeAndValue[i] + " in test case: " + f); attila@963: } attila@963: } attila@963: } else { attila@963: fail("Number of Deoptimizing recompilation is not correct, expected: " + expectedValues.length + " and found: " + events.length + " in test case: " + f); attila@963: } attila@963: } attila@963: attila@963: checkExpectedRecompilation(function divisionByZeroTest() {var x = { a: 2, b:1 }; x.a = Number.POSITIVE_INFINITY; x.b = 0; print(x.a/x.b); return 1;}, attila@963: expectedValues =[['double', 'Infinity']]); attila@963: checkExpectedRecompilation(function divisionWithRemainderTest() {var x = { a: 7, b:2 }; print(x.a/x.b); return 1;}, expectedValues =[['double', '3.5']]); attila@963: checkExpectedRecompilation(function infinityMultiplicationTest() {var x = { a: Number.POSITIVE_INFINITY, b: Number.POSITIVE_INFINITY}; print(x.a*x.b); return 1;}, attila@963: expectedValues =[['double', 'Infinity']]); attila@963: checkExpectedRecompilation(function maxValueMultiplicationTest() {var x = { a: Number.MAX_VALUE, b: Number.MAX_VALUE}; print(x.a*x.b); return 1;}, attila@963: expectedValues =[['double', '1.7976931348623157e+308']]); attila@963: checkExpectedRecompilation(function divisionByInfinityTest() {var x = { a: -1, b: Number.POSITIVE_INFINITY}; print(x.a/x.b); return 1;}, attila@963: expectedValues =[['double', 'Infinity']]); attila@963: checkExpectedRecompilation(function divisionByStringTest() {var x = { a: Number.POSITIVE_INFINITY, b: 'Hello'}; print(x.a/x.b); return 1;}, attila@963: expectedValues =[['double', 'Infinity']]); attila@963: checkExpectedRecompilation(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;}, attila@963: expectedValues =[['object', 'undefined']]); attila@963: checkExpectedRecompilation(function functionTest(a,b,c) { d = (a + b) * c; print(d); return 1;}, expectedValues =[['double', 'NaN']]); attila@963: checkExpectedRecompilation(function andTest(a,b) { d = a && b; print(d); return 1;}, expectedValues =[['object', 'undefined']]);