sundar@1412: /* sundar@1482: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. sundar@1412: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@1482: * sundar@1412: * This code is free software; you can redistribute it and/or modify it sundar@1412: * under the terms of the GNU General Public License version 2 only, as sundar@1412: * published by the Free Software Foundation. sundar@1482: * sundar@1412: * This code is distributed in the hope that it will be useful, but WITHOUT sundar@1412: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sundar@1412: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sundar@1412: * version 2 for more details (a copy is included in the LICENSE file that sundar@1412: * accompanied this code). sundar@1482: * sundar@1412: * You should have received a copy of the GNU General Public License version sundar@1412: * 2 along with this work; if not, write to the Free Software Foundation, sundar@1412: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@1482: * sundar@1412: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sundar@1412: * or visit www.oracle.com if you need additional information or have any sundar@1412: * questions. sundar@1412: */ sundar@1412: sundar@1412: /** sundar@1412: * JDK-8098578: Global scope is not accessible with indirect load call sundar@1412: * sundar@1412: * @test sundar@1412: * @run sundar@1412: */ sundar@1412: sundar@1412: var obj = { foo: 343 }; sundar@1412: var global = this; sundar@1412: var x = 434; sundar@1412: sundar@1412: // indirect load call sundar@1412: var res = load.call(obj, { sundar@1412: name: "t.js", sundar@1412: // global is accessible. All declarations go into sundar@1412: // intermediate inaccessible scope. "this" is global sundar@1412: // User's passed object's properties are accessible sundar@1412: // as variables. sundar@1412: script: "foo -= 300; var bar = x; Assert.assertTrue(bar == 434); function func() {}; this" sundar@1412: }) sundar@1412: sundar@1412: // 'this' for the evaluated code is global sundar@1412: Assert.assertTrue(res === global); sundar@1412: sundar@1412: // properties of passed object are accessible in evaluated code sundar@1412: Assert.assertTrue(obj.foo == 43); sundar@1412: sundar@1412: // vars, functions definined in evaluated code don't go into passed object sundar@1412: Assert.assertTrue(typeof obj.bar == "undefined"); sundar@1412: Assert.assertTrue(typeof obj.func == "undefined"); sundar@1412: sundar@1412: // vars, functions definined in evaluated code don't go leak into global sundar@1412: Assert.assertTrue(typeof bar == "undefined"); sundar@1412: Assert.assertTrue(typeof func == "undefined"); sundar@1412: Assert.assertTrue(typeof foo == "undefined"); sundar@1412: sundar@1412: var res = load.call(undefined, { sundar@1412: name: "t1.js", sundar@1412: // still global is accessible and 'this' is global sundar@1412: script: "Assert.assertTrue(x == 434); this" sundar@1412: }); sundar@1412: sundar@1412: // indirect load with 'undefined' this is same as as direct load sundar@1412: // or load on global itself. sundar@1412: Assert.assertTrue(res === global); sundar@1412: sundar@1412: // indirect load with 'undefined' this is same as as direct load sundar@1412: // or load on global itself. sundar@1412: var res = load.call(null, { sundar@1412: name: "t2.js", sundar@1412: // still global is accessible and 'this' is global sundar@1412: script: "Assert.assertTrue(x == 434); this" sundar@1412: }); sundar@1412: Assert.assertTrue(res === global); sundar@1412: sundar@1412: // indirect load with mirror object sundar@1412: var mirror = loadWithNewGlobal({ sundar@1412: name: "t3.js", sundar@1412: script: "({ foo: 'hello', x: Math.PI })" sundar@1412: }); sundar@1412: sundar@1412: var res = load.call(mirror, { sundar@1412: name: "t4.js", sundar@1412: script: "Assert.assertTrue(foo == 'hello'); Assert.assertTrue(x == Math.PI); this" sundar@1412: }); sundar@1412: Assert.assertTrue(res === global); sundar@1412: sundar@1412: // indirect load on non-script object, non-mirror results in TypeError sundar@1412: function tryLoad(obj) { sundar@1412: try { sundar@1412: load.call(obj, { sundar@1412: name: "t5.js", script: "this" sundar@1412: }); sundar@1412: throw new Error("should thrown TypeError for: " + obj); sundar@1412: } catch (e if TypeError) {} sundar@1412: } sundar@1412: sundar@1412: tryLoad("hello"); sundar@1412: tryLoad(Math.E); sundar@1412: tryLoad(true); sundar@1412: tryLoad(false); sundar@1412: sundar@1412: // indirect load of a large script sundar@1412: load.call({}, __DIR__ + "JDK-8098807-payload.js");