test/script/basic/JDK-8047764.js

changeset 1020
9ee8fd4a7266
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/script/basic/JDK-8047764.js	Mon Sep 22 13:28:28 2014 +0200
     1.3 @@ -0,0 +1,94 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + * 
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + * 
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + * 
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + * 
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * JDK-8047764: Indexed or polymorphic set on global affects Object.prototype
    1.29 + *
    1.30 + * @test
    1.31 + * @run
    1.32 + */
    1.33 +
    1.34 +// Test global set operation on properties defined in Object.prototype
    1.35 +
    1.36 +Object.defineProperty(Object.prototype, "prop1", { get: function() { return 1; }, set: function(v) { print("setting prop1: " + v); }});
    1.37 +Object.defineProperty(Object.prototype, "prop2", { value: 1, writable: false, configurable: false });
    1.38 +
    1.39 +try {
    1.40 +    prop1 = 1;
    1.41 +    print("prop 1: " + prop2);
    1.42 +} catch (e) {
    1.43 +    print(e.name);
    1.44 +}
    1.45 +
    1.46 +try {
    1.47 +    prop2 = 2;
    1.48 +    print("prop 2: " + prop2);
    1.49 +} catch (e) {
    1.50 +    print(e.name);
    1.51 +}
    1.52 +
    1.53 +// Make sure various ways of setting global toString don't affect Object.prototype.toString
    1.54 +
    1.55 +function checkToString() {
    1.56 +    print(global);
    1.57 +    print(Object.prototype);
    1.58 +    print(global.toString === Object.prototype.toString);
    1.59 +    print(objProtoToString === Object.prototype.toString);
    1.60 +}
    1.61 +
    1.62 +var global = this;
    1.63 +var objProtoToString = Object.prototype.toString;
    1.64 +global["toString"] = function() { return "global toString 1"; };
    1.65 +checkToString();
    1.66 +global.toString = function() { return "global toString 2"; };
    1.67 +checkToString();
    1.68 +toString = function() { return "global toString 3"; };
    1.69 +checkToString();
    1.70 +
    1.71 +// Test setters on 'with' object
    1.72 +
    1.73 +var p = { prop3: 3, toString: function() { return "[object p]"; }};
    1.74 +Object.defineProperty(p, "prop4", { get: function() { print("get", this); return 4; }, set: function(v) { print("set", this, v); }});
    1.75 +var o = Object.create(p);
    1.76 +o.toString = function() { return "[object o]"; };
    1.77 +
    1.78 +with(o) {
    1.79 +    (function() {
    1.80 +        var m = 5;
    1.81 +        (function() {
    1.82 +            print(prop3);
    1.83 +            prop3 = m;
    1.84 +            print(prop3);
    1.85 +            print(prop4);
    1.86 +            prop4 = m;
    1.87 +            print(prop4);
    1.88 +        })();
    1.89 +    })();
    1.90 +}
    1.91 +
    1.92 +print(o.hasOwnProperty("prop3"));
    1.93 +print(o.prop3);
    1.94 +print(p.prop3);
    1.95 +print(o.hasOwnProperty("prop4"));
    1.96 +print(o.prop4);
    1.97 +print(p.prop4);

mercurial