author | sundar |
Wed, 06 May 2015 20:04:42 +0530 | |
changeset 30394 | 72a59e4dffea |
parent 26765 | 97501edd2979 |
permissions | -rw-r--r-- |
26765
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
1 |
/* |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
4 |
* |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
8 |
* |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
13 |
* accompanied this code). |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
14 |
* |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
18 |
* |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
21 |
* questions. |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
22 |
*/ |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
23 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
24 |
/** |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
25 |
* JDK-8047764: Indexed or polymorphic set on global affects Object.prototype |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
26 |
* |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
27 |
* @test |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
28 |
* @run |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
29 |
*/ |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
30 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
31 |
// Test global set operation on properties defined in Object.prototype |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
32 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
33 |
Object.defineProperty(Object.prototype, "prop1", { get: function() { return 1; }, set: function(v) { print("setting prop1: " + v); }}); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
34 |
Object.defineProperty(Object.prototype, "prop2", { value: 1, writable: false, configurable: false }); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
35 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
36 |
try { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
37 |
prop1 = 1; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
38 |
print("prop 1: " + prop2); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
39 |
} catch (e) { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
40 |
print(e.name); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
41 |
} |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
42 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
43 |
try { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
44 |
prop2 = 2; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
45 |
print("prop 2: " + prop2); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
46 |
} catch (e) { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
47 |
print(e.name); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
48 |
} |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
49 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
50 |
// Make sure various ways of setting global toString don't affect Object.prototype.toString |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
51 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
52 |
function checkToString() { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
53 |
print(global); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
54 |
print(Object.prototype); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
55 |
print(global.toString === Object.prototype.toString); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
56 |
print(objProtoToString === Object.prototype.toString); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
57 |
} |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
58 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
59 |
var global = this; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
60 |
var objProtoToString = Object.prototype.toString; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
61 |
global["toString"] = function() { return "global toString 1"; }; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
62 |
checkToString(); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
63 |
global.toString = function() { return "global toString 2"; }; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
64 |
checkToString(); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
65 |
toString = function() { return "global toString 3"; }; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
66 |
checkToString(); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
67 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
68 |
// Test setters on 'with' object |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
69 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
70 |
var p = { prop3: 3, toString: function() { return "[object p]"; }}; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
71 |
Object.defineProperty(p, "prop4", { get: function() { print("get", this); return 4; }, set: function(v) { print("set", this, v); }}); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
72 |
var o = Object.create(p); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
73 |
o.toString = function() { return "[object o]"; }; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
74 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
75 |
with(o) { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
76 |
(function() { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
77 |
var m = 5; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
78 |
(function() { |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
79 |
print(prop3); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
80 |
prop3 = m; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
81 |
print(prop3); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
82 |
print(prop4); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
83 |
prop4 = m; |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
84 |
print(prop4); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
85 |
})(); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
86 |
})(); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
87 |
} |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
88 |
|
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
89 |
print(o.hasOwnProperty("prop3")); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
90 |
print(o.prop3); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
91 |
print(p.prop3); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
92 |
print(o.hasOwnProperty("prop4")); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
93 |
print(o.prop4); |
97501edd2979
8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents:
diff
changeset
|
94 |
print(p.prop4); |