test/script/basic/JDK-8134609.js

Tue, 21 Mar 2017 13:41:57 -0700

author
asaha
date
Tue, 21 Mar 2017 13:41:57 -0700
changeset 2160
1df40fe54cd6
parent 1548
898e2a08a252
permissions
-rw-r--r--

Merge

hannesw@1548 1 /*
hannesw@1548 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
hannesw@1548 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
hannesw@1548 4 *
hannesw@1548 5 * This code is free software; you can redistribute it and/or modify it
hannesw@1548 6 * under the terms of the GNU General Public License version 2 only, as
hannesw@1548 7 * published by the Free Software Foundation.
hannesw@1548 8 *
hannesw@1548 9 * This code is distributed in the hope that it will be useful, but WITHOUT
hannesw@1548 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
hannesw@1548 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
hannesw@1548 12 * version 2 for more details (a copy is included in the LICENSE file that
hannesw@1548 13 * accompanied this code).
hannesw@1548 14 *
hannesw@1548 15 * You should have received a copy of the GNU General Public License version
hannesw@1548 16 * 2 along with this work; if not, write to the Free Software Foundation,
hannesw@1548 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
hannesw@1548 18 *
hannesw@1548 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
hannesw@1548 20 * or visit www.oracle.com if you need additional information or have any
hannesw@1548 21 * questions.
hannesw@1548 22 */
hannesw@1548 23
hannesw@1548 24 /**
hannesw@1548 25 * JDK-8134609: Allow constructors with same prototoype map to share the allocator map
hannesw@1548 26 *
hannesw@1548 27 * @test
hannesw@1548 28 * @run
hannesw@1548 29 * @fork
hannesw@1548 30 * @option -Dnashorn.debug
hannesw@1548 31 */
hannesw@1548 32
hannesw@1548 33 function createProto(members) {
hannesw@1548 34 function P() {
hannesw@1548 35 for (var id in members) {
hannesw@1548 36 if (members.hasOwnProperty(id)) {
hannesw@1548 37 this[id] = members[id];
hannesw@1548 38 }
hannesw@1548 39 }
hannesw@1548 40 return this;
hannesw@1548 41 }
hannesw@1548 42 return new P();
hannesw@1548 43 }
hannesw@1548 44
hannesw@1548 45 function createSubclass(prototype, members) {
hannesw@1548 46 function C() {
hannesw@1548 47 for (var id in members) {
hannesw@1548 48 if (members.hasOwnProperty(id)) {
hannesw@1548 49 this[id] = members[id];
hannesw@1548 50 }
hannesw@1548 51 }
hannesw@1548 52 return this;
hannesw@1548 53 }
hannesw@1548 54
hannesw@1548 55 C.prototype = prototype;
hannesw@1548 56
hannesw@1548 57 return new C();
hannesw@1548 58 }
hannesw@1548 59
hannesw@1548 60 function assertP1(object, value) {
hannesw@1548 61 Assert.assertTrue(object.p1 === value);
hannesw@1548 62 }
hannesw@1548 63
hannesw@1548 64 // First prototype will have non-shared proto-map. Second and third will be shared.
hannesw@1548 65 var proto0 = createProto({p1: 0, p2: 1});
hannesw@1548 66 var proto1 = createProto({p1: 1, p2: 2});
hannesw@1548 67 var proto2 = createProto({p1: 2, p2: 3});
hannesw@1548 68
hannesw@1548 69 Assert.assertTrue(Debug.map(proto1) === Debug.map(proto2));
hannesw@1548 70
hannesw@1548 71 assertP1(proto1, 1);
hannesw@1548 72 assertP1(proto2, 2);
hannesw@1548 73
hannesw@1548 74 // First instantiation will have a non-shared prototype map, from the second one
hannesw@1548 75 // maps will be shared until a different proto map comes along.
hannesw@1548 76 var child0 = createSubclass(proto1, {c1: 1, c2: 2});
hannesw@1548 77 var child1 = createSubclass(proto2, {c1: 2, c2: 3});
hannesw@1548 78 var child2 = createSubclass(proto1, {c1: 3, c2: 4});
hannesw@1548 79 var child3 = createSubclass(proto2, {c1: 1, c2: 2});
hannesw@1548 80 var child4 = createSubclass(proto0, {c1: 3, c2: 2});
hannesw@1548 81
hannesw@1548 82 Assert.assertTrue(Debug.map(child1) === Debug.map(child2));
hannesw@1548 83 Assert.assertTrue(Debug.map(child1) === Debug.map(child3));
hannesw@1548 84 Assert.assertTrue(Debug.map(child3) !== Debug.map(child4));
hannesw@1548 85
hannesw@1548 86 assertP1(child1, 2);
hannesw@1548 87 assertP1(child2, 1);
hannesw@1548 88 assertP1(child3, 2);
hannesw@1548 89 assertP1(child4, 0);
hannesw@1548 90
hannesw@1548 91 Assert.assertTrue(delete proto2.p1);
hannesw@1548 92
hannesw@1548 93 assertP1(child3, undefined);
hannesw@1548 94 assertP1(child2, 1);
hannesw@1548 95 Assert.assertTrue(Debug.map(child1) !== Debug.map(child3));

mercurial