1 /*
   2  * Copyright (c) 2015, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import java.lang.invoke.SwitchPoint;
  29 
  30 /**
  31  * This class represents a property map that can be shared among multiple prototype objects, allowing all inheriting
  32  * top-level objects to also share one property map. This class is only used for prototype objects, the
  33  * top-level objects use ordinary {@link PropertyMap}s with the {@link PropertyMap#sharedProtoMap} field
  34  * set to the expected shared prototype map.
  35  *
  36  * <p>When an instance of this class is evolved because a property is added, removed, or modified in an object
  37  * using it, the {@link #invalidateSwitchPoint()} method is invoked to signal to all callsites and inheriting
  38  * objects that the assumption of a single shared prototype map is no longer valid. The property map resulting
  39  * from the modification will no longer be an instance of this class.</p>
  40  */
  41 public final class SharedPropertyMap extends PropertyMap {
  42 
  43     @SuppressWarnings("serial") // Not statically typed as Serializable
  44     private SwitchPoint switchPoint;
  45 
  46     private static final long serialVersionUID = 2166297719721778876L;
  47 
  48     /**
  49      * Create a new shared property map from the given {@code map}.
  50      * @param map property map to copy
  51      */
  52     SharedPropertyMap(final PropertyMap map) {
  53         super(map);
  54         this.switchPoint = new SwitchPoint();
  55     }
  56 
  57     @Override
  58     public void propertyChanged(final Property property) {
  59         invalidateSwitchPoint();
  60         super.propertyChanged(property);
  61     }
  62 
  63     @Override
  64     synchronized boolean isValidSharedProtoMap() {
  65         return switchPoint != null;
  66     }
  67 
  68     @Override
  69     synchronized SwitchPoint getSharedProtoSwitchPoint() {
  70         return switchPoint;
  71     }
  72 
  73     /**
  74      * Invalidate the shared prototype switch point if this is a shared prototype map.
  75      */
  76     synchronized void invalidateSwitchPoint() {
  77         if (switchPoint != null) {
  78             assert !switchPoint.hasBeenInvalidated();
  79             SwitchPoint.invalidateAll(new SwitchPoint[]{ switchPoint });
  80             switchPoint = null;
  81         }
  82     }
  83 }