1 /*
   2  * Copyright (c) 2015, 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  * JDK-8136544: Call site switching to megamorphic causes incorrect property read
  26  *
  27  * @test
  28  * @option --unstable-relink-threshold=8
  29  * @run
  30  */
  31 
  32 var ScriptContext = Java.type("javax.script.ScriptContext");
  33 var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
  34 var m = new ScriptEngineManager();
  35 var e = m.getEngineByName("nashorn");
  36 
  37 var scope = e.getBindings(ScriptContext.ENGINE_SCOPE);
  38 var MYVAR = "myvar";
  39 
  40 function loopupVar() {
  41     try {
  42         e.eval(MYVAR);
  43         return true;
  44     } catch (e) {
  45         return false;
  46     }
  47 }
  48 
  49 // make sure we exercise callsite beyond megamorphic threshold we set
  50 // in this test via nashorn.unstable.relink.threshold property
  51 // In each iteration, callsite is exercised twice (two evals)
  52 // So, LIMIT should be more than 4 to exercise megamorphic callsites.
  53 
  54 var LIMIT = 5; // This LIMIT should be more than 4
  55 
  56 for (var i = 0; i < LIMIT; i++) {
  57     // remove the variable and lookup
  58     delete scope[MYVAR];
  59     Assert.assertFalse(loopupVar(), "Expected true in iteration " + i);
  60 
  61     // set that variable and check again
  62     scope[MYVAR] = "foo";
  63     Assert.assertTrue(loopupVar(), "Expected false in iteration " + i);
  64 }