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.  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     private SwitchPoint switchPoint;
  44 
  45     private static final long serialVersionUID = 2166297719721778876L;
  46 
  47     /**
  48      * Create a new shared property map from the given {@code map}.
  49      * @param map property map to copy
  50      */
  51     SharedPropertyMap(final PropertyMap map) {
  52         super(map);
  53         this.switchPoint = new SwitchPoint();
  54     }
  55 
  56     @Override
  57     public void invalidateProperty(final Property property) {
  58         invalidateSwitchPoint();
  59         super.invalidateProperty(property);
  60     }
  61 
  62     @Override
  63     synchronized boolean isValidSharedProtoMap() {
  64         return switchPoint != null;
  65     }
  66 
  67     @Override
  68     synchronized SwitchPoint getSharedProtoSwitchPoint() {
  69         return switchPoint;
  70     }
  71 
  72     /**
  73      * Invalidate the shared prototype switch point if this is a shared prototype map.
  74      */
  75     synchronized void invalidateSwitchPoint() {
  76         if (switchPoint != null) {
  77             assert !switchPoint.hasBeenInvalidated();
  78             SwitchPoint.invalidateAll(new SwitchPoint[]{ switchPoint });
  79             switchPoint = null;
  80         }
  81     }
  82 }