JDK-8016618.js revision 837:ddda121eca56
1/* 2 * Copyright (c) 2010, 2013, 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-8016618: script mirror object access should be improved 26 * 27 * @test 28 * @option -scripting 29 * @option -strict 30 * @run 31 */ 32 33var global = loadWithNewGlobal({ 34 name: "code", 35 script: <<EOF 36var x = 33; 37 38function func(x, y) { 39 print('func.x = ' + x); 40 print('func.x = ' + y) 41}; 42 43var obj = { 44 foo: 'hello', 45 bar: 42 46}; 47 48Object.defineProperty(obj, "bar", 49 { enumerable: false, writable: false }); 50 51// return global 52this; 53EOF 54}); 55 56// load on mirror with local object as argument 57global.load({ 58 name: "code", 59 script: "print('x = ' + x)" 60}); 61 62function f() { 63 // mirror function called with local arguments 64 // global.func.apply(obj, arguments); 65 global.func.apply(obj, arguments); 66} 67 68f(23, "hello"); 69 70f(24, "hello2"); 71 72var oldCall = Function.prototype.call; 73Function.prototype.call = function() { 74 throw "this should never happen! go back to apply!"; 75}; 76 77f(25, "hello3"); 78 79Function.prototype.call = oldCall; 80 81var fObject = global.eval("Object"); 82 83// instanceof on mirrors 84print("global instanceof fObject? " + (global instanceof fObject)); 85 86// Object API on mirrors 87 88var desc = Object.getOwnPropertyDescriptor(global, "x"); 89print("x is wriable ? " + desc.writable); 90print("x value = " + desc.value); 91 92var proto = Object.getPrototypeOf(global); 93print("global's __proto__ " + proto); 94 95var obj = global.obj; 96var keys = Object.keys(obj); 97print("Object.keys on obj"); 98for (var i in keys) { 99 print(keys[i]); 100} 101 102print("Object.getOwnProperties on obj"); 103var keys = Object.getOwnPropertyNames(obj); 104for (var i in keys) { 105 print(keys[i]); 106} 107 108// mirror array access 109var array = global.eval("[334, 55, 65]"); 110Array.prototype.forEach.call(array, function(elem) { 111 print("forEach " + elem) 112}); 113 114print("reduceRight " + Array.prototype.reduceRight.call(array, 115 function(previousValue, currentValue, index, array) { 116 print("reduceRight cur value " + currentValue); 117 return previousValue + currentValue; 118}, 0)); 119 120print("reduce " + Array.prototype.reduce.call(array, 121 function(previousValue, currentValue, index, array) { 122 print("reduce cur value " + currentValue); 123 return previousValue + currentValue; 124}, 0)); 125 126print("forEach"); 127Array.prototype.forEach.call(array, function(o) { 128 print(o); 129}); 130 131print("Array.isArray(array)? " + Array.isArray(array)); 132 133// try to write to a non-writable property of mirror 134try { 135 obj.bar = 33; 136} catch (e) { 137 print(e); 138} 139 140// mirror function called with local callback 141print("forEach on mirror"); 142array.forEach(function(toto) { 143 print(toto); 144}); 145