NashornGuards.java revision 1551:f3b883bec2d0
1/*
2 * Copyright (c) 2010, 2013, 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.linker;
27
28import static jdk.nashorn.internal.lookup.Lookup.MH;
29
30import java.lang.invoke.MethodHandle;
31import java.lang.invoke.MethodHandles;
32import java.lang.ref.WeakReference;
33import jdk.dynalink.CallSiteDescriptor;
34import jdk.dynalink.linker.LinkRequest;
35import jdk.nashorn.api.scripting.JSObject;
36import jdk.nashorn.internal.objects.Global;
37import jdk.nashorn.internal.runtime.Property;
38import jdk.nashorn.internal.runtime.PropertyMap;
39import jdk.nashorn.internal.runtime.ScriptFunction;
40import jdk.nashorn.internal.runtime.ScriptObject;
41import jdk.nashorn.internal.runtime.options.Options;
42
43/**
44 * Constructor of method handles used to guard call sites.
45 */
46public final class NashornGuards {
47    private static final MethodHandle IS_MAP              = findOwnMH("isMap", boolean.class, ScriptObject.class, PropertyMap.class);
48    private static final MethodHandle IS_MAP_SCRIPTOBJECT = findOwnMH("isMap", boolean.class, Object.class, PropertyMap.class);
49    private static final MethodHandle IS_INSTANCEOF_2     = findOwnMH("isInstanceOf2", boolean.class, Object.class, Class.class, Class.class);
50    private static final MethodHandle IS_SCRIPTOBJECT     = findOwnMH("isScriptObject", boolean.class, Object.class);
51    private static final MethodHandle IS_NOT_JSOBJECT     = findOwnMH("isNotJSObject", boolean.class, Object.class);
52    private static final MethodHandle SAME_OBJECT       = findOwnMH("sameObject", boolean.class, Object.class, WeakReference.class);
53    //TODO - maybe put this back in ScriptFunction instead of the ClassCastException.class relinkage
54    //private static final MethodHandle IS_SCRIPTFUNCTION = findOwnMH("isScriptFunction", boolean.class, Object.class);
55
56    private static final boolean CCE_ONLY = Options.getBooleanProperty("nashorn.cce");
57
58    // don't create me!
59    private NashornGuards() {
60    }
61
62    /**
63     * Given a callsite descriptor and a link request, determine whether we should use an instanceof
64     * check explicitly for the guard if needed, or if we should link it with a try/catch ClassCastException
65     * combinator as its relink criteria - i.e. relink when CCE is thrown.
66     *
67     * @param desc     callsite descriptor
68     * @param request  link request
69     * @return true of explicit instanceof check is needed
70     */
71    public static boolean explicitInstanceOfCheck(final CallSiteDescriptor desc, final LinkRequest request) {
72        //THIS is currently true, as the inliner encounters several problems with sun.misc.ValueConversions.castReference
73        //otherwise. We should only use the exception based relink where we have no choice, and the result is faster code,
74        //for example in the NativeArray, TypedArray, ContinuousArray getters. For the standard callsite, it appears that
75        //we lose performance rather than gain it, due to JVM issues. :-(
76        return !CCE_ONLY;
77    }
78
79    /**
80     * Returns a guard that does an instanceof ScriptObject check on the receiver
81     * @return guard
82     */
83    public static MethodHandle getScriptObjectGuard() {
84        return IS_SCRIPTOBJECT;
85    }
86
87   /**
88    * Get the guard that checks if an item is not a {@code JSObject}
89    * @return method handle for guard
90    */
91   public static MethodHandle getNotJSObjectGuard() {
92       return IS_NOT_JSOBJECT;
93   }
94
95    /**
96     * Returns a guard that does an instanceof ScriptObject check on the receiver
97     * @param explicitInstanceOfCheck - if false, then this is a nop, because it's all the guard does
98     * @return guard
99     */
100    public static MethodHandle getScriptObjectGuard(final boolean explicitInstanceOfCheck) {
101        return explicitInstanceOfCheck ? IS_SCRIPTOBJECT : null;
102    }
103
104    /**
105     * Get the guard that checks if a {@link PropertyMap} is equal to
106     * a known map, using reference comparison
107     *
108     * @param explicitInstanceOfCheck true if we should do an explicit script object instanceof check instead of just casting
109     * @param map The map to check against. This will be bound to the guard method handle
110     *
111     * @return method handle for guard
112     */
113    public static MethodHandle getMapGuard(final PropertyMap map, final boolean explicitInstanceOfCheck) {
114        return MH.insertArguments(explicitInstanceOfCheck ? IS_MAP_SCRIPTOBJECT : IS_MAP, 1, map);
115    }
116
117    /**
118     * Determine whether the given callsite needs a guard.
119     * @param property the property, or null
120     * @param desc the callsite descriptor
121     * @return true if a guard should be used for this callsite
122     */
123    static boolean needsGuard(final Property property, final CallSiteDescriptor desc) {
124        return property == null || property.isConfigurable()
125                || property.isBound() || property.hasDualFields()
126                || !NashornCallSiteDescriptor.isFastScope(desc) || property.canChangeType();
127    }
128
129    /**
130     * Get the guard for a property access. This returns an identity guard for non-configurable global properties
131     * and a map guard for everything else.
132     *
133     * @param sobj the first object in the prototype chain
134     * @param property the property
135     * @param desc the callsite descriptor
136     * @param explicitInstanceOfCheck true if we should do an explicit script object instanceof check instead of just casting
137     * @return method handle for guard
138     */
139    public static MethodHandle getGuard(final ScriptObject sobj, final Property property, final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck) {
140        if (!needsGuard(property, desc)) {
141            return null;
142        }
143        if (NashornCallSiteDescriptor.isScope(desc)) {
144            if (property != null && property.isBound() && !property.canChangeType()) {
145                // This is a declared top level variables in main script or eval, use identity guard.
146                return getIdentityGuard(sobj);
147            }
148            if (!(sobj instanceof Global) && (property == null || property.isConfigurable())) {
149                // Undeclared variables in nested evals need stronger guards
150                return combineGuards(getIdentityGuard(sobj), getMapGuard(sobj.getMap(), explicitInstanceOfCheck));
151            }
152        }
153        return getMapGuard(sobj.getMap(), explicitInstanceOfCheck);
154    }
155
156
157    /**
158     * Get a guard that checks referential identity of the current object.
159     *
160     * @param sobj the self object
161     * @return true if same self object instance
162     */
163    public static MethodHandle getIdentityGuard(final ScriptObject sobj) {
164        return MH.insertArguments(SAME_OBJECT, 1, new WeakReference<>(sobj));
165    }
166
167    /**
168     * Get a guard that checks if in item is an instance of either of two classes.
169     *
170     * @param class1 the first class
171     * @param class2 the second class
172     * @return method handle for guard
173     */
174    public static MethodHandle getInstanceOf2Guard(final Class<?> class1, final Class<?> class2) {
175        return MH.insertArguments(IS_INSTANCEOF_2, 1, class1, class2);
176    }
177
178    /**
179     * Combine two method handles of type {@code (Object)boolean} using logical AND.
180     *
181     * @param guard1 the first guard
182     * @param guard2 the second guard, only invoked if guard1 returns true
183     * @return true if both guard1 and guard2 returned true
184     */
185    public static MethodHandle combineGuards(final MethodHandle guard1, final MethodHandle guard2) {
186        if (guard1 == null) {
187            return guard2;
188        } else if (guard2 == null) {
189            return guard1;
190        } else {
191            return MH.guardWithTest(guard1, guard2, MH.dropArguments(MH.constant(boolean.class, false), 0, Object.class));
192        }
193    }
194
195    @SuppressWarnings("unused")
196    private static boolean isScriptObject(final Object self) {
197        return self instanceof ScriptObject;
198    }
199
200    @SuppressWarnings("unused")
201    private static boolean isScriptObject(final Class<? extends ScriptObject> clazz, final Object self) {
202        return clazz.isInstance(self);
203    }
204
205    @SuppressWarnings("unused")
206    private static boolean isMap(final ScriptObject self, final PropertyMap map) {
207        return self.getMap() == map;
208    }
209
210    @SuppressWarnings("unused")
211    private static boolean isNotJSObject(final Object self) {
212        return !(self instanceof JSObject);
213    }
214
215    @SuppressWarnings("unused")
216    private static boolean isMap(final Object self, final PropertyMap map) {
217        return self instanceof ScriptObject && ((ScriptObject)self).getMap() == map;
218    }
219
220
221    @SuppressWarnings("unused")
222    private static boolean sameObject(final Object self, final WeakReference<ScriptObject> ref) {
223        return self == ref.get();
224    }
225
226    @SuppressWarnings("unused")
227    private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) {
228        return class1.isInstance(self) || class2.isInstance(self);
229    }
230
231    @SuppressWarnings("unused")
232    private static boolean isScriptFunction(final Object self) {
233        return self instanceof ScriptFunction;
234    }
235
236    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
237        return MH.findStatic(MethodHandles.lookup(), NashornGuards.class, name, MH.type(rtype, types));
238    }
239}
240