hannesw@1548: /* hannesw@1548: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. hannesw@1548: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. hannesw@1548: * hannesw@1548: * This code is free software; you can redistribute it and/or modify it hannesw@1548: * under the terms of the GNU General Public License version 2 only, as hannesw@1548: * published by the Free Software Foundation. hannesw@1548: * hannesw@1548: * This code is distributed in the hope that it will be useful, but WITHOUT hannesw@1548: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or hannesw@1548: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License hannesw@1548: * version 2 for more details (a copy is included in the LICENSE file that hannesw@1548: * accompanied this code). hannesw@1548: * hannesw@1548: * You should have received a copy of the GNU General Public License version hannesw@1548: * 2 along with this work; if not, write to the Free Software Foundation, hannesw@1548: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. hannesw@1548: * hannesw@1548: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA hannesw@1548: * or visit www.oracle.com if you need additional information or have any hannesw@1548: * questions. hannesw@1548: */ hannesw@1548: hannesw@1548: /** hannesw@1548: * JDK-8134609: Allow constructors with same prototoype map to share the allocator map hannesw@1548: * hannesw@1548: * @test hannesw@1548: * @run hannesw@1548: * @fork hannesw@1548: * @option -Dnashorn.debug hannesw@1548: */ hannesw@1548: hannesw@1548: function createProto(members) { hannesw@1548: function P() { hannesw@1548: for (var id in members) { hannesw@1548: if (members.hasOwnProperty(id)) { hannesw@1548: this[id] = members[id]; hannesw@1548: } hannesw@1548: } hannesw@1548: return this; hannesw@1548: } hannesw@1548: return new P(); hannesw@1548: } hannesw@1548: hannesw@1548: function createSubclass(prototype, members) { hannesw@1548: function C() { hannesw@1548: for (var id in members) { hannesw@1548: if (members.hasOwnProperty(id)) { hannesw@1548: this[id] = members[id]; hannesw@1548: } hannesw@1548: } hannesw@1548: return this; hannesw@1548: } hannesw@1548: hannesw@1548: C.prototype = prototype; hannesw@1548: hannesw@1548: return new C(); hannesw@1548: } hannesw@1548: hannesw@1548: function assertP1(object, value) { hannesw@1548: Assert.assertTrue(object.p1 === value); hannesw@1548: } hannesw@1548: hannesw@1548: // First prototype will have non-shared proto-map. Second and third will be shared. hannesw@1548: var proto0 = createProto({p1: 0, p2: 1}); hannesw@1548: var proto1 = createProto({p1: 1, p2: 2}); hannesw@1548: var proto2 = createProto({p1: 2, p2: 3}); hannesw@1548: hannesw@1548: Assert.assertTrue(Debug.map(proto1) === Debug.map(proto2)); hannesw@1548: hannesw@1548: assertP1(proto1, 1); hannesw@1548: assertP1(proto2, 2); hannesw@1548: hannesw@1548: // First instantiation will have a non-shared prototype map, from the second one hannesw@1548: // maps will be shared until a different proto map comes along. hannesw@1548: var child0 = createSubclass(proto1, {c1: 1, c2: 2}); hannesw@1548: var child1 = createSubclass(proto2, {c1: 2, c2: 3}); hannesw@1548: var child2 = createSubclass(proto1, {c1: 3, c2: 4}); hannesw@1548: var child3 = createSubclass(proto2, {c1: 1, c2: 2}); hannesw@1548: var child4 = createSubclass(proto0, {c1: 3, c2: 2}); hannesw@1548: hannesw@1548: Assert.assertTrue(Debug.map(child1) === Debug.map(child2)); hannesw@1548: Assert.assertTrue(Debug.map(child1) === Debug.map(child3)); hannesw@1548: Assert.assertTrue(Debug.map(child3) !== Debug.map(child4)); hannesw@1548: hannesw@1548: assertP1(child1, 2); hannesw@1548: assertP1(child2, 1); hannesw@1548: assertP1(child3, 2); hannesw@1548: assertP1(child4, 0); hannesw@1548: hannesw@1548: Assert.assertTrue(delete proto2.p1); hannesw@1548: hannesw@1548: assertP1(child3, undefined); hannesw@1548: assertP1(child2, 1); hannesw@1548: Assert.assertTrue(Debug.map(child1) !== Debug.map(child3));