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 
  33 var global = loadWithNewGlobal({
  34     name: "code",
  35     script: <<EOF
  36 var x = 33;
  37 
  38 function func(x, y) {
  39     print('func.x = ' + x);
  40     print('func.x = ' + y)
  41 }; 
  42 
  43 var obj = {
  44     foo: 'hello',
  45     bar: 42
  46 };
  47 
  48 Object.defineProperty(obj, "bar",
  49     { enumerable: false, writable: false });
  50 
  51 // return global
  52 this;
  53 EOF
  54 });
  55 
  56 // load on mirror with local object as argument
  57 global.load({ 
  58     name: "code",
  59     script: "print('x = ' + x)"
  60 });
  61 
  62 function f() {
  63     // mirror function called with local arguments
  64     global.func.apply(obj, arguments);
  65 }
  66 
  67 f(23, "hello");
  68 
  69 var fObject = global.eval("Object");
  70 
  71 // instanceof on mirrors
  72 print("global instanceof fObject? " + (global instanceof fObject));
  73 
  74 // Object API on mirrors
  75 
  76 var desc = Object.getOwnPropertyDescriptor(global, "x");
  77 print("x is wriable ? " + desc.writable);
  78 print("x value = " + desc.value);
  79 
  80 var proto = Object.getPrototypeOf(global);
  81 print("global's __proto__ " + proto);
  82 
  83 var obj = global.obj;
  84 var keys = Object.keys(obj);
  85 print("Object.keys on obj");
  86 for (var i in keys) {
  87     print(keys[i]);
  88 }
  89 
  90 print("Object.getOwnProperties on obj");
  91 var keys = Object.getOwnPropertyNames(obj);
  92 for (var i in keys) {
  93   print(keys[i]);
  94 }
  95 
  96 // mirror array access
  97 var array = global.eval("[334, 55, 65]");
  98 Array.prototype.forEach.call(array, function(elem) {
  99     print("forEach " + elem)
 100 });
 101 
 102 print("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 
 108 print("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 
 114 print("forEach");
 115 Array.prototype.forEach.call(array, function(o) {
 116    print(o);
 117 });
 118 
 119 print("Array.isArray(array)? " + Array.isArray(array));
 120 
 121 // try to write to a non-writable property of mirror
 122 try {
 123    obj.bar = 33;
 124 } catch (e) {
 125    print(e);
 126 }
 127 
 128 // mirror function called with local callback
 129 print("forEach on mirror");
 130 array.forEach(function(toto) {
 131     print(toto);
 132 });