JDK-8047764.js revision 1033:c1f651636d9c
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
33Object.defineProperty(Object.prototype, "prop1", { get: function() { return 1; }, set: function(v) { print("setting prop1: " + v); }});
34Object.defineProperty(Object.prototype, "prop2", { value: 1, writable: false, configurable: false });
35
36try {
37    prop1 = 1;
38    print("prop 1: " + prop2);
39} catch (e) {
40    print(e.name);
41}
42
43try {
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
52function checkToString() {
53    print(global);
54    print(Object.prototype);
55    print(global.toString === Object.prototype.toString);
56    print(objProtoToString === Object.prototype.toString);
57}
58
59var global = this;
60var objProtoToString = Object.prototype.toString;
61global["toString"] = function() { return "global toString 1"; };
62checkToString();
63global.toString = function() { return "global toString 2"; };
64checkToString();
65toString = function() { return "global toString 3"; };
66checkToString();
67
68// Test setters on 'with' object
69
70var p = { prop3: 3, toString: function() { return "[object p]"; }};
71Object.defineProperty(p, "prop4", { get: function() { print("get", this); return 4; }, set: function(v) { print("set", this, v); }});
72var o = Object.create(p);
73o.toString = function() { return "[object o]"; };
74
75with(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
89print(o.hasOwnProperty("prop3"));
90print(o.prop3);
91print(p.prop3);
92print(o.hasOwnProperty("prop4"));
93print(o.prop4);
94print(p.prop4);
95