test/nashorn/script/basic/JDK-8016618.js
changeset 47216 71c04702a3d5
parent 24778 2ff5d7041566
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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     global.func.apply(obj, arguments);
       
    66 }
       
    67 
       
    68 f(23, "hello");
       
    69 
       
    70 f(24, "hello2");
       
    71 
       
    72 var oldCall = Function.prototype.call;
       
    73 Function.prototype.call = function() {
       
    74     throw "this should never happen! go back to apply!";
       
    75 };
       
    76 
       
    77 f(25, "hello3");
       
    78 
       
    79 Function.prototype.call = oldCall;
       
    80 
       
    81 var fObject = global.eval("Object");
       
    82 
       
    83 // instanceof on mirrors
       
    84 print("global instanceof fObject? " + (global instanceof fObject));
       
    85 
       
    86 // Object API on mirrors
       
    87 
       
    88 var desc = Object.getOwnPropertyDescriptor(global, "x");
       
    89 print("x is wriable ? " + desc.writable);
       
    90 print("x value = " + desc.value);
       
    91 
       
    92 var proto = Object.getPrototypeOf(global);
       
    93 print("global's __proto__ " + proto);
       
    94 
       
    95 var obj = global.obj;
       
    96 var keys = Object.keys(obj);
       
    97 print("Object.keys on obj");
       
    98 for (var i in keys) {
       
    99     print(keys[i]);
       
   100 }
       
   101 
       
   102 print("Object.getOwnProperties on obj");
       
   103 var keys = Object.getOwnPropertyNames(obj);
       
   104 for (var i in keys) {
       
   105   print(keys[i]);
       
   106 }
       
   107 
       
   108 // mirror array access
       
   109 var array = global.eval("[334, 55, 65]");
       
   110 Array.prototype.forEach.call(array, function(elem) {
       
   111     print("forEach " + elem)
       
   112 });
       
   113 
       
   114 print("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 
       
   120 print("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 
       
   126 print("forEach");
       
   127 Array.prototype.forEach.call(array, function(o) {
       
   128    print(o);
       
   129 });
       
   130 
       
   131 print("Array.isArray(array)? " + Array.isArray(array));
       
   132 
       
   133 // try to write to a non-writable property of mirror
       
   134 try {
       
   135    obj.bar = 33;
       
   136 } catch (e) {
       
   137    print(e);
       
   138 }
       
   139 
       
   140 // mirror function called with local callback
       
   141 print("forEach on mirror");
       
   142 array.forEach(function(toto) {
       
   143     print(toto);
       
   144 });