JDK-8134609.js revision 1429:b4eb53200105
1228753Smm/*
2228753Smm * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3228753Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4228753Smm *
5228753Smm * This code is free software; you can redistribute it and/or modify it
6228753Smm * under the terms of the GNU General Public License version 2 only, as
7228753Smm * published by the Free Software Foundation.
8228753Smm *
9228753Smm * This code is distributed in the hope that it will be useful, but WITHOUT
10228753Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11228753Smm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12228753Smm * version 2 for more details (a copy is included in the LICENSE file that
13228753Smm * accompanied this code).
14228753Smm *
15228753Smm * You should have received a copy of the GNU General Public License version
16228753Smm * 2 along with this work; if not, write to the Free Software Foundation,
17228753Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18228753Smm *
19228753Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20228753Smm * or visit www.oracle.com if you need additional information or have any
21228753Smm * questions.
22228753Smm */
23228753Smm
24228753Smm/**
25228753Smm * JDK-8134609: Allow constructors with same prototoype map to share the allocator map
26228753Smm *
27228763Smm * @test
28228753Smm * @run
29228753Smm * @fork
30228753Smm * @option -Dnashorn.debug
31228753Smm */
32228753Smm
33228753Smmfunction createProto(members) {
34228753Smm    function P() {
35228753Smm        for (var id in members) {
36228753Smm            if (members.hasOwnProperty(id)) {
37228753Smm                this[id] = members[id];
38228753Smm            }
39228753Smm        }
40228753Smm        return this;
41228753Smm    }
42228753Smm    return new P();
43228753Smm}
44228753Smm
45228753Smmfunction createSubclass(prototype, members) {
46228753Smm    function C() {
47228753Smm        for (var id in members) {
48228753Smm            if (members.hasOwnProperty(id)) {
49228753Smm                this[id] = members[id];
50228753Smm            }
51228753Smm        }
52228753Smm        return this;
53228753Smm    }
54228753Smm
55228753Smm    C.prototype = prototype;
56228753Smm
57228753Smm    return new C();
58228753Smm}
59228753Smm
60228753Smmfunction assertP1(object, value) {
61228753Smm    Assert.assertTrue(object.p1 === value);
62228753Smm}
63228753Smm
64228753Smm// First prototype will have non-shared proto-map. Second and third will be shared.
65228753Smmvar proto0 = createProto({p1: 0, p2: 1});
66228753Smmvar proto1 = createProto({p1: 1, p2: 2});
67228753Smmvar proto2 = createProto({p1: 2, p2: 3});
68228753Smm
69228753SmmAssert.assertTrue(Debug.map(proto1) === Debug.map(proto2));
70228753Smm
71228753SmmassertP1(proto1, 1);
72228753SmmassertP1(proto2, 2);
73228753Smm
74228753Smm// First instantiation will have a non-shared prototype map, from the second one
75232153Smm// maps will be shared until a different proto map comes along.
76232153Smmvar child0 = createSubclass(proto1, {c1: 1, c2: 2});
77228753Smmvar child1 = createSubclass(proto2, {c1: 2, c2: 3});
78228753Smmvar child2 = createSubclass(proto1, {c1: 3, c2: 4});
79228753Smmvar child3 = createSubclass(proto2, {c1: 1, c2: 2});
80228753Smmvar child4 = createSubclass(proto0, {c1: 3, c2: 2});
81228753Smm
82228753SmmAssert.assertTrue(Debug.map(child1) === Debug.map(child2));
83228753SmmAssert.assertTrue(Debug.map(child1) === Debug.map(child3));
84228753SmmAssert.assertTrue(Debug.map(child3) !== Debug.map(child4));
85228753Smm
86302001SmmassertP1(child1, 2);
87228753SmmassertP1(child2, 1);
88228753SmmassertP1(child3, 2);
89228753SmmassertP1(child4, 0);
90228753Smm
91228753SmmAssert.assertTrue(delete proto2.p1);
92228753Smm
93228753SmmassertP1(child3, undefined);
94228753SmmassertP1(child2, 1);
95228753SmmAssert.assertTrue(Debug.map(child1) !== Debug.map(child3));
96228753Smm