1 /*
   2  * Copyright (c) 2015, 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-8134609: Allow constructors with same prototoype map to share the allocator map
  26  *
  27  * @test
  28  * @run
  29  * @fork
  30  * @option -Dnashorn.debug
  31  */
  32 
  33 function createProto(members) {
  34     function P() {
  35         for (var id in members) {
  36             if (members.hasOwnProperty(id)) {
  37                 this[id] = members[id];
  38             }
  39         }
  40         return this;
  41     }
  42     return new P();
  43 }
  44 
  45 function createSubclass(prototype, members) {
  46     function C() {
  47         for (var id in members) {
  48             if (members.hasOwnProperty(id)) {
  49                 this[id] = members[id];
  50             }
  51         }
  52         return this;
  53     }
  54 
  55     C.prototype = prototype;
  56 
  57     return new C();
  58 }
  59 
  60 function assertP1(object, value) {
  61     Assert.assertTrue(object.p1 === value);
  62 }
  63 
  64 // First prototype will have non-shared proto-map. Second and third will be shared.
  65 var proto0 = createProto({p1: 0, p2: 1});
  66 var proto1 = createProto({p1: 1, p2: 2});
  67 var proto2 = createProto({p1: 2, p2: 3});
  68 
  69 Assert.assertTrue(Debug.map(proto1) === Debug.map(proto2));
  70 
  71 assertP1(proto1, 1);
  72 assertP1(proto2, 2);
  73 
  74 // First instantiation will have a non-shared prototype map, from the second one
  75 // maps will be shared until a different proto map comes along.
  76 var child0 = createSubclass(proto1, {c1: 1, c2: 2});
  77 var child1 = createSubclass(proto2, {c1: 2, c2: 3});
  78 var child2 = createSubclass(proto1, {c1: 3, c2: 4});
  79 var child3 = createSubclass(proto2, {c1: 1, c2: 2});
  80 var child4 = createSubclass(proto0, {c1: 3, c2: 2});
  81 
  82 Assert.assertTrue(Debug.map(child1) === Debug.map(child2));
  83 Assert.assertTrue(Debug.map(child1) === Debug.map(child3));
  84 Assert.assertTrue(Debug.map(child3) !== Debug.map(child4));
  85 
  86 assertP1(child1, 2);
  87 assertP1(child2, 1);
  88 assertP1(child3, 2);
  89 assertP1(child4, 0);
  90 
  91 Assert.assertTrue(delete proto2.p1);
  92 
  93 assertP1(child3, undefined);
  94 assertP1(child2, 1);
  95 Assert.assertTrue(Debug.map(child1) !== Debug.map(child3));