test/nashorn/script/basic/JDK-8136544.js
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 33686 nashorn/test/script/basic/JDK-8136544.js@1391474a6405
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32694
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     1
/*
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     4
 *
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     8
 *
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    13
 * accompanied this code).
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    14
 *
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    18
 *
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    21
 * questions.
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    22
 */
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    23
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    24
/**
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    25
 * JDK-8136544: Call site switching to megamorphic causes incorrect property read
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    26
 *
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    27
 * @test
33686
1391474a6405 8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents: 32694
diff changeset
    28
 * @option --unstable-relink-threshold=8
32694
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    29
 * @run
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    30
 */
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    31
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    32
var ScriptContext = Java.type("javax.script.ScriptContext");
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    33
var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    34
var m = new ScriptEngineManager();
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    35
var e = m.getEngineByName("nashorn");
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    36
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    37
var scope = e.getBindings(ScriptContext.ENGINE_SCOPE);
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    38
var MYVAR = "myvar";
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    39
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    40
function loopupVar() {
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    41
    try {
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    42
        e.eval(MYVAR);
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    43
        return true;
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    44
    } catch (e) {
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    45
        return false;
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    46
    }
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    47
}
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    48
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    49
// make sure we exercise callsite beyond megamorphic threshold we set
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    50
// in this test via nashorn.unstable.relink.threshold property
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    51
// In each iteration, callsite is exercised twice (two evals)
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    52
// So, LIMIT should be more than 4 to exercise megamorphic callsites.
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    53
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    54
var LIMIT = 5; // This LIMIT should be more than 4
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    55
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    56
for (var i = 0; i < LIMIT; i++) {
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    57
    // remove the variable and lookup
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    58
    delete scope[MYVAR];
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    59
    Assert.assertFalse(loopupVar(), "Expected true in iteration " + i);
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    60
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    61
    // set that variable and check again
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    62
    scope[MYVAR] = "foo";
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    63
    Assert.assertTrue(loopupVar(), "Expected false in iteration " + i);
da2f35ab2ea6 8136544: Call site switching to megamorphic causes incorrect property read
sundar
parents:
diff changeset
    64
}