test/nashorn/script/basic/JDK-8047764.js
changeset 47216 71c04702a3d5
parent 26765 97501edd2979
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2014, 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-8047764: Indexed or polymorphic set on global affects Object.prototype
       
    26  *
       
    27  * @test
       
    28  * @run
       
    29  */
       
    30 
       
    31 // Test global set operation on properties defined in Object.prototype
       
    32 
       
    33 Object.defineProperty(Object.prototype, "prop1", { get: function() { return 1; }, set: function(v) { print("setting prop1: " + v); }});
       
    34 Object.defineProperty(Object.prototype, "prop2", { value: 1, writable: false, configurable: false });
       
    35 
       
    36 try {
       
    37     prop1 = 1;
       
    38     print("prop 1: " + prop2);
       
    39 } catch (e) {
       
    40     print(e.name);
       
    41 }
       
    42 
       
    43 try {
       
    44     prop2 = 2;
       
    45     print("prop 2: " + prop2);
       
    46 } catch (e) {
       
    47     print(e.name);
       
    48 }
       
    49 
       
    50 // Make sure various ways of setting global toString don't affect Object.prototype.toString
       
    51 
       
    52 function checkToString() {
       
    53     print(global);
       
    54     print(Object.prototype);
       
    55     print(global.toString === Object.prototype.toString);
       
    56     print(objProtoToString === Object.prototype.toString);
       
    57 }
       
    58 
       
    59 var global = this;
       
    60 var objProtoToString = Object.prototype.toString;
       
    61 global["toString"] = function() { return "global toString 1"; };
       
    62 checkToString();
       
    63 global.toString = function() { return "global toString 2"; };
       
    64 checkToString();
       
    65 toString = function() { return "global toString 3"; };
       
    66 checkToString();
       
    67 
       
    68 // Test setters on 'with' object
       
    69 
       
    70 var p = { prop3: 3, toString: function() { return "[object p]"; }};
       
    71 Object.defineProperty(p, "prop4", { get: function() { print("get", this); return 4; }, set: function(v) { print("set", this, v); }});
       
    72 var o = Object.create(p);
       
    73 o.toString = function() { return "[object o]"; };
       
    74 
       
    75 with(o) {
       
    76     (function() {
       
    77         var m = 5;
       
    78         (function() {
       
    79             print(prop3);
       
    80             prop3 = m;
       
    81             print(prop3);
       
    82             print(prop4);
       
    83             prop4 = m;
       
    84             print(prop4);
       
    85         })();
       
    86     })();
       
    87 }
       
    88 
       
    89 print(o.hasOwnProperty("prop3"));
       
    90 print(o.prop3);
       
    91 print(p.prop3);
       
    92 print(o.hasOwnProperty("prop4"));
       
    93 print(o.prop4);
       
    94 print(p.prop4);