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
26package jdk.nashorn.internal.runtime;
27
28import 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 */
41public 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    public SharedPropertyMap(final PropertyMap map) {
52        super(map);
53        this.switchPoint = new SwitchPoint();
54    }
55
56    @Override
57    public void propertyAdded(final Property property, final boolean isSelf) {
58        if (isSelf) {
59            invalidateSwitchPoint();
60        }
61        super.propertyAdded(property, isSelf);
62    }
63
64    @Override
65    public void propertyDeleted(final Property property, final boolean isSelf) {
66        if (isSelf) {
67            invalidateSwitchPoint();
68        }
69        super.propertyDeleted(property, isSelf);
70    }
71
72    @Override
73    public void propertyModified(final Property oldProperty, final Property newProperty, final boolean isSelf) {
74        if (isSelf) {
75            invalidateSwitchPoint();
76        }
77        super.propertyModified(oldProperty, newProperty, isSelf);
78    }
79
80    @Override
81    synchronized boolean isValidSharedProtoMap() {
82        return switchPoint != null;
83    }
84
85    @Override
86    synchronized SwitchPoint getSharedProtoSwitchPoint() {
87        return switchPoint;
88    }
89
90    /**
91     * Invalidate the shared prototype switch point if this is a shared prototype map.
92     */
93    synchronized void invalidateSwitchPoint() {
94        if (switchPoint != null) {
95            assert !switchPoint.hasBeenInvalidated();
96            SwitchPoint.invalidateAll(new SwitchPoint[]{ switchPoint });
97            switchPoint = null;
98        }
99    }
100}
101