sundar@1534: /* sundar@1534: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. sundar@1534: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@1534: * sundar@1534: * This code is free software; you can redistribute it and/or modify it sundar@1534: * under the terms of the GNU General Public License version 2 only, as sundar@1534: * published by the Free Software Foundation. sundar@1534: * sundar@1534: * This code is distributed in the hope that it will be useful, but WITHOUT sundar@1534: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sundar@1534: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sundar@1534: * version 2 for more details (a copy is included in the LICENSE file that sundar@1534: * accompanied this code). sundar@1534: * sundar@1534: * You should have received a copy of the GNU General Public License version sundar@1534: * 2 along with this work; if not, write to the Free Software Foundation, sundar@1534: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@1534: * sundar@1534: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sundar@1534: * or visit www.oracle.com if you need additional information or have any sundar@1534: * questions. sundar@1534: */ sundar@1534: sundar@1534: /** sundar@1534: * JDK-8136544: Call site switching to megamorphic causes incorrect property read sundar@1534: * sundar@1534: * @test sundar@1534: * @fork sundar@1534: * @option -Dnashorn.unstable.relink.threshold=8 sundar@1534: * @run sundar@1534: */ sundar@1534: sundar@1534: var ScriptContext = Java.type("javax.script.ScriptContext"); sundar@1534: var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager"); sundar@1534: var m = new ScriptEngineManager(); sundar@1534: var e = m.getEngineByName("nashorn"); sundar@1534: sundar@1534: var scope = e.getBindings(ScriptContext.ENGINE_SCOPE); sundar@1534: var MYVAR = "myvar"; sundar@1534: sundar@1534: function loopupVar() { sundar@1534: try { sundar@1534: e.eval(MYVAR); sundar@1534: return true; sundar@1534: } catch (e) { sundar@1534: return false; sundar@1534: } sundar@1534: } sundar@1534: sundar@1534: // make sure we exercise callsite beyond megamorphic threshold we set sundar@1534: // in this test via nashorn.unstable.relink.threshold property sundar@1534: // In each iteration, callsite is exercised twice (two evals) sundar@1534: // So, LIMIT should be more than 4 to exercise megamorphic callsites. sundar@1534: sundar@1534: var LIMIT = 5; // This LIMIT should be more than 4 sundar@1534: sundar@1534: for (var i = 0; i < LIMIT; i++) { sundar@1534: // remove the variable and lookup sundar@1534: delete scope[MYVAR]; sundar@1534: Assert.assertFalse(loopupVar(), "Expected true in iteration " + i); sundar@1534: sundar@1534: // set that variable and check again sundar@1534: scope[MYVAR] = "foo"; sundar@1534: Assert.assertTrue(loopupVar(), "Expected false in iteration " + i); sundar@1534: }