nashorn/test/script/basic/JDK-8098578.js
author hannesw
Fri, 03 Feb 2017 13:28:00 +0100
changeset 43555 1bb10bccf057
parent 31925 85c87bc2f1f3
permissions -rw-r--r--
8173888: Test for JDK-8169481 causes stack overflows in parser tests Reviewed-by: jlaskey, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31196
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
     1
/*
31925
85c87bc2f1f3 8132092: Nashorn copyright has to be updated
sundar
parents: 31196
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
31196
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
31925
85c87bc2f1f3 8132092: Nashorn copyright has to be updated
sundar
parents: 31196
diff changeset
     4
 *
31196
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
31925
85c87bc2f1f3 8132092: Nashorn copyright has to be updated
sundar
parents: 31196
diff changeset
     8
 *
31196
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    13
 * accompanied this code).
31925
85c87bc2f1f3 8132092: Nashorn copyright has to be updated
sundar
parents: 31196
diff changeset
    14
 *
31196
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31925
85c87bc2f1f3 8132092: Nashorn copyright has to be updated
sundar
parents: 31196
diff changeset
    18
 *
31196
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    21
 * questions.
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    22
 */
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    23
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    24
/**
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    25
 * JDK-8098578: Global scope is not accessible with indirect load call
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    26
 *
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    27
 * @test
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    28
 * @run
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    29
 */
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    30
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    31
var obj = { foo: 343 };
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    32
var global = this;
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    33
var x = 434;
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    34
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    35
// indirect load call
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    36
var res = load.call(obj, {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    37
   name: "t.js",
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    38
   // global is accessible. All declarations go into
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    39
   // intermediate inaccessible scope. "this" is global
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    40
   // User's passed object's properties are accessible
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    41
   // as variables.
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    42
   script: "foo -= 300; var bar = x; Assert.assertTrue(bar == 434); function func() {}; this"
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    43
})
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    44
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    45
// 'this' for the evaluated code is global
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    46
Assert.assertTrue(res === global);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    47
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    48
// properties of passed object are accessible in evaluated code
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    49
Assert.assertTrue(obj.foo == 43);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    50
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    51
// vars, functions definined in evaluated code don't go into passed object
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    52
Assert.assertTrue(typeof obj.bar == "undefined");
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    53
Assert.assertTrue(typeof obj.func == "undefined");
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    54
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    55
// vars, functions definined in evaluated code don't go leak into global
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    56
Assert.assertTrue(typeof bar == "undefined");
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    57
Assert.assertTrue(typeof func == "undefined");
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    58
Assert.assertTrue(typeof foo == "undefined");
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    59
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    60
var res = load.call(undefined, {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    61
    name: "t1.js",
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    62
    // still global is accessible and 'this' is global
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    63
    script: "Assert.assertTrue(x == 434); this"
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    64
});
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    65
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    66
// indirect load with 'undefined' this is same as as direct load
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    67
// or load on global itself.
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    68
Assert.assertTrue(res === global);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    69
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    70
// indirect load with 'undefined' this is same as as direct load
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    71
// or load on global itself.
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    72
var res = load.call(null, {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    73
    name: "t2.js",
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    74
    // still global is accessible and 'this' is global
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    75
    script: "Assert.assertTrue(x == 434); this"
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    76
});
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    77
Assert.assertTrue(res === global);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    78
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    79
// indirect load with mirror object
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    80
var mirror = loadWithNewGlobal({
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    81
    name: "t3.js",
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    82
    script: "({ foo: 'hello', x: Math.PI })"
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    83
});
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    84
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    85
var res = load.call(mirror, {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    86
    name: "t4.js",
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    87
    script: "Assert.assertTrue(foo == 'hello'); Assert.assertTrue(x == Math.PI); this"
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    88
});
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    89
Assert.assertTrue(res === global);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    90
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    91
// indirect load on non-script object, non-mirror results in TypeError
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    92
function tryLoad(obj) {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    93
    try {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    94
        load.call(obj, {
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    95
            name: "t5.js", script: "this"
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    96
        });
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    97
        throw new Error("should thrown TypeError for: " + obj);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    98
    } catch (e if TypeError) {}
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
    99
}
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   100
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   101
tryLoad("hello");
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   102
tryLoad(Math.E);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   103
tryLoad(true);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   104
tryLoad(false);
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   105
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   106
// indirect load of a large script
2ab121636a13 8098578: Global scope is not accessible with indirect load call
sundar
parents:
diff changeset
   107
load.call({}, __DIR__ + "JDK-8098807-payload.js");