JDK-8016618.js revision 349:3d947baa33cc
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} 66 67f(23, "hello"); 68 69var fObject = global.eval("Object"); 70 71// instanceof on mirrors 72print("global instanceof fObject? " + (global instanceof fObject)); 73 74// Object API on mirrors 75 76var desc = Object.getOwnPropertyDescriptor(global, "x"); 77print("x is wriable ? " + desc.writable); 78print("x value = " + desc.value); 79 80var proto = Object.getPrototypeOf(global); 81print("global's __proto__ " + proto); 82 83var obj = global.obj; 84var keys = Object.keys(obj); 85print("Object.keys on obj"); 86for (var i in keys) { 87 print(keys[i]); 88} 89 90print("Object.getOwnProperties on obj"); 91var keys = Object.getOwnPropertyNames(obj); 92for (var i in keys) { 93 print(keys[i]); 94} 95 96// mirror array access 97var array = global.eval("[334, 55, 65]"); 98Array.prototype.forEach.call(array, function(elem) { 99 print("forEach " + elem) 100}); 101 102print("reduceRight " + Array.prototype.reduceRight.call(array, 103 function(previousValue, currentValue, index, array) { 104 print("reduceRight cur value " + currentValue); 105 return previousValue + currentValue; 106}, 0)); 107 108print("reduce " + Array.prototype.reduce.call(array, 109 function(previousValue, currentValue, index, array) { 110 print("reduce cur value " + currentValue); 111 return previousValue + currentValue; 112}, 0)); 113 114print("forEach"); 115Array.prototype.forEach.call(array, function(o) { 116 print(o); 117}); 118 119print("Array.isArray(array)? " + Array.isArray(array)); 120 121// try to write to a non-writable property of mirror 122try { 123 obj.bar = 33; 124} catch (e) { 125 print(e); 126} 127 128// mirror function called with local callback 129print("forEach on mirror"); 130array.forEach(function(toto) { 131 print(toto); 132}); 133