Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/trusted/optimistic_recompilation.js
32285 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* Ask Debug for an event log of favourable events instead of using --log flags printing to screen25* @test26* @bug 8037086,803839827* @fork28* @option -Dnashorn.debug=true29* @option --log=recompile:quiet30* @option --optimistic-types=true31*/3233var forName = java.lang.Class["forName(String)"];34var RuntimeEvent = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static;35var getValue = RuntimeEvent.class.getMethod("getValue");36var RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static;37var getReturnType = RewriteException.class.getMethod("getReturnType");38var RecompilationEvent = forName("jdk.nashorn.internal.runtime.events.RecompilationEvent").static;39var getReturnValue = RecompilationEvent.class.getMethod("getReturnValue");40var setReturnTypeAndValue = [];41var expectedValues = [];4243function checkExpectedRecompilation(f, expectedValues, testCase) {44Debug.clearRuntimeEvents();45print(f());46events = Debug.getRuntimeEvents();47//make sure we got runtime events48print("events = " + (events.toString().indexOf("RuntimeEvent") != -1));49if (events.length == expectedValues.length) {50for (var i in events) {51var e = events[i];52var returnValue = getReturnValue.invoke(e);53if (typeof returnValue != 'undefined') {54setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue];55} else {56returnValue = "undefined";57setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue];58}59if (!setReturnTypeAndValue[i].toString().equals(expectedValues[i].toString())) {60fail("The return values are not as expected. Expected value: " + expectedValues[i] + " and got: " + setReturnTypeAndValue[i] + " in test case: " + f);61}62}63} else {64fail("Number of Deoptimizing recompilation is not correct, expected: " + expectedValues.length + " and found: " + events.length + " in test case: " + f);65}66}6768checkExpectedRecompilation(function divisionByZeroTest() {var x = { a: 2, b:1 }; x.a = Number.POSITIVE_INFINITY; x.b = 0; print(x.a/x.b); return 1;},69expectedValues =[['double', 'Infinity']]);70checkExpectedRecompilation(function divisionWithRemainderTest() {var x = { a: 7, b:2 }; print(x.a/x.b); return 1;}, expectedValues =[['double', '3.5']]);71checkExpectedRecompilation(function infinityMultiplicationTest() {var x = { a: Number.POSITIVE_INFINITY, b: Number.POSITIVE_INFINITY}; print(x.a*x.b); return 1;},72expectedValues =[['double', 'Infinity']]);73checkExpectedRecompilation(function maxValueMultiplicationTest() {var x = { a: Number.MAX_VALUE, b: Number.MAX_VALUE}; print(x.a*x.b); return 1;},74expectedValues =[['double', '1.7976931348623157e+308']]);75checkExpectedRecompilation(function divisionByInfinityTest() {var x = { a: -1, b: Number.POSITIVE_INFINITY}; print(x.a/x.b); return 1;},76expectedValues =[['double', 'Infinity']]);77checkExpectedRecompilation(function divisionByStringTest() {var x = { a: Number.POSITIVE_INFINITY, b: 'Hello'}; print(x.a/x.b); return 1;},78expectedValues =[['double', 'Infinity']]);79checkExpectedRecompilation(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;},80expectedValues =[['object', 'undefined']]);81checkExpectedRecompilation(function functionTest(a,b,c) { d = (a + b) * c; print(d); return 1;}, expectedValues =[['double', 'NaN']]);82checkExpectedRecompilation(function andTest(a,b) { d = a && b; print(d); return 1;}, expectedValues =[['object', 'undefined']]);838485