UserAccessorProperty.java revision 1036:f0b5e3900a10
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;
27
28import static jdk.nashorn.internal.lookup.Lookup.MH;
29import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
30import static jdk.nashorn.internal.runtime.JSType.CONVERT_OBJECT_OPTIMISTIC;
31import static jdk.nashorn.internal.runtime.JSType.getAccessorTypeIndex;
32import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
33
34import java.lang.invoke.MethodHandle;
35import java.lang.invoke.MethodHandles;
36import java.lang.invoke.MethodType;
37import java.util.concurrent.Callable;
38import jdk.nashorn.internal.lookup.Lookup;
39import jdk.nashorn.internal.runtime.linker.Bootstrap;
40
41/**
42 * Property with user defined getters/setters. Actual getter and setter
43 * functions are stored in underlying ScriptObject. Only the 'slot' info is
44 * stored in the property.
45 */
46public final class UserAccessorProperty extends SpillProperty {
47
48    private static final long serialVersionUID = -5928687246526840321L;
49
50    static final class Accessors {
51        Object getter;
52        Object setter;
53
54        Accessors(final Object getter, final Object setter) {
55            set(getter, setter);
56        }
57
58        final void set(final Object getter, final Object setter) {
59            this.getter = getter;
60            this.setter = setter;
61        }
62
63        @Override
64        public String toString() {
65            return "[getter=" + getter + " setter=" + setter + ']';
66        }
67    }
68
69    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
70
71    /** Getter method handle */
72    private final static MethodHandle INVOKE_GETTER_ACCESSOR = findOwnMH_S("invokeGetterAccessor", Object.class, Accessors.class, Object.class);
73
74    /** Setter method handle */
75    private final static MethodHandle INVOKE_SETTER_ACCESSOR = findOwnMH_S("invokeSetterAccessor", void.class, Accessors.class, String.class, Object.class, Object.class);
76
77    /** Dynamic invoker for getter */
78    private static final Object GETTER_INVOKER_KEY = new Object();
79
80    private static MethodHandle getINVOKE_UA_GETTER() {
81
82        return Context.getGlobal().getDynamicInvoker(GETTER_INVOKER_KEY,
83                new Callable<MethodHandle>() {
84                    @Override
85                    public MethodHandle call() {
86                        return Bootstrap.createDynamicInvoker("dyn:call", Object.class,
87                            Object.class, Object.class);
88                    }
89                });
90    }
91
92    /** Dynamic invoker for setter */
93    private static Object SETTER_INVOKER_KEY = new Object();
94
95    private static MethodHandle getINVOKE_UA_SETTER() {
96        return Context.getGlobal().getDynamicInvoker(SETTER_INVOKER_KEY,
97                new Callable<MethodHandle>() {
98                    @Override
99                    public MethodHandle call() {
100                        return Bootstrap.createDynamicInvoker("dyn:call", void.class,
101                            Object.class, Object.class, Object.class);
102                    }
103                });
104    }
105
106    /**
107     * Constructor
108     *
109     * @param key   property key
110     * @param flags property flags
111     * @param slot  spill slot
112     */
113    UserAccessorProperty(final String key, final int flags, final int slot) {
114        super(key, flags, slot);
115    }
116
117    private UserAccessorProperty(final UserAccessorProperty property) {
118        super(property);
119    }
120
121    private UserAccessorProperty(final UserAccessorProperty property, final Class<?> newType) {
122        super(property, newType);
123    }
124
125    @Override
126    public Property copy() {
127        return new UserAccessorProperty(this);
128    }
129
130    @Override
131    public Property copy(final Class<?> newType) {
132        return new UserAccessorProperty(this, newType);
133    }
134
135    void setAccessors(final ScriptObject sobj, final PropertyMap map, final Accessors gs) {
136        try {
137            //invoke the getter and find out
138            super.getSetter(Object.class, map).invokeExact((Object)sobj, (Object)gs);
139        } catch (final Error | RuntimeException t) {
140            throw t;
141        } catch (final Throwable t) {
142            throw new RuntimeException(t);
143        }
144    }
145
146    //pick the getter setter out of the correct spill slot in sobj
147    Accessors getAccessors(final ScriptObject sobj) {
148        try {
149            //invoke the super getter with this spill slot
150            //get the getter setter from the correct spill slot
151            final Object gs = super.getGetter(Object.class).invokeExact((Object)sobj);
152            return (Accessors)gs;
153        } catch (final Error | RuntimeException t) {
154            throw t;
155        } catch (final Throwable t) {
156            throw new RuntimeException(t);
157        }
158    }
159
160    @Override
161    public Class<?> getCurrentType() {
162        return Object.class;
163    }
164
165    @Override
166    public boolean hasGetterFunction(final ScriptObject sobj) {
167        return getAccessors(sobj).getter != null;
168    }
169
170    @Override
171    public boolean hasSetterFunction(final ScriptObject sobj) {
172        return getAccessors(sobj).setter != null;
173    }
174
175    @Override
176    public int getIntValue(final ScriptObject self, final ScriptObject owner) {
177        return (int)getObjectValue(self, owner);
178    }
179
180    @Override
181    public long getLongValue(final ScriptObject self, final ScriptObject owner) {
182        return (long)getObjectValue(self, owner);
183    }
184
185    @Override
186    public double getDoubleValue(final ScriptObject self, final ScriptObject owner) {
187        return (double)getObjectValue(self, owner);
188    }
189
190    @Override
191    public Object getObjectValue(final ScriptObject self, final ScriptObject owner) {
192        return invokeGetterAccessor(getAccessors((owner != null) ? owner : self), self);
193    }
194
195    @Override
196    public void setValue(final ScriptObject self, final ScriptObject owner, final int value, final boolean strict) {
197        setValue(self, owner, (Object) value, strict);
198    }
199
200    @Override
201    public void setValue(final ScriptObject self, final ScriptObject owner, final long value, final boolean strict) {
202        setValue(self, owner, (Object) value, strict);
203    }
204
205    @Override
206    public void setValue(final ScriptObject self, final ScriptObject owner, final double value, final boolean strict) {
207        setValue(self, owner, (Object) value, strict);
208    }
209
210    @Override
211    public void setValue(final ScriptObject self, final ScriptObject owner, final Object value, final boolean strict) {
212        invokeSetterAccessor(getAccessors((owner != null) ? owner : self), strict ? getKey() : null, self, value);
213    }
214
215    @Override
216    public MethodHandle getGetter(final Class<?> type) {
217        //this returns a getter on the format (Accessors, Object receiver)
218        return Lookup.filterReturnType(INVOKE_GETTER_ACCESSOR, type);
219    }
220
221    @Override
222    public MethodHandle getOptimisticGetter(final Class<?> type, final int programPoint) {
223        //fortype is always object, but in the optimistic world we have to throw
224        //unwarranted optimism exception for narrower types. We can improve this
225        //by checking for boxed types and unboxing them, but it is doubtful that
226        //this gives us any performance, as UserAccessorProperties are typically not
227        //primitives. Are there? TODO: investigate later. For now we just throw an
228        //exception for narrower types than object
229
230        if (type.isPrimitive()) {
231            final MethodHandle getter = getGetter(Object.class);
232            final MethodHandle mh =
233                    MH.asType(
234                            MH.filterReturnValue(
235                                    getter,
236                                    MH.insertArguments(
237                                            CONVERT_OBJECT_OPTIMISTIC.get(getAccessorTypeIndex(type)),
238                                            1,
239                                            programPoint)),
240                                    getter.type().changeReturnType(type));
241
242            return mh;
243        }
244
245        assert type == Object.class;
246        return getGetter(type);
247    }
248
249    @Override
250    void initMethodHandles(final Class<?> structure) {
251        throw new UnsupportedOperationException();
252    }
253
254    @Override
255    public ScriptFunction getGetterFunction(final ScriptObject sobj) {
256        final Object value = getAccessors(sobj).getter;
257        return (value instanceof ScriptFunction) ? (ScriptFunction)value : null;
258    }
259
260    @Override
261    public MethodHandle getSetter(final Class<?> type, final PropertyMap currentMap) {
262        return INVOKE_SETTER_ACCESSOR;
263    }
264
265    @Override
266    public ScriptFunction getSetterFunction(final ScriptObject sobj) {
267        final Object value = getAccessors(sobj).setter;
268        return (value instanceof ScriptFunction) ? (ScriptFunction)value : null;
269    }
270
271    /**
272     * Get the getter for the {@code Accessors} object.
273     * This is the the super {@code Object} type getter with {@code Accessors} return type.
274     *
275     * @return The getter handle for the Accessors
276     */
277    MethodHandle getAccessorsGetter() {
278        return super.getGetter(Object.class).asType(MethodType.methodType(Accessors.class, Object.class));
279    }
280
281    // User defined getter and setter are always called by "dyn:call". Note that the user
282    // getter/setter may be inherited. If so, proto is bound during lookup. In either
283    // inherited or self case, slot is also bound during lookup. Actual ScriptFunction
284    // to be called is retrieved everytime and applied.
285    private static Object invokeGetterAccessor(final Accessors gs, final Object self) {
286        final Object func = gs.getter;
287        if (func instanceof ScriptFunction) {
288            try {
289                return getINVOKE_UA_GETTER().invokeExact(func, self);
290            } catch (final Error | RuntimeException t) {
291                throw t;
292            } catch (final Throwable t) {
293                throw new RuntimeException(t);
294            }
295        }
296
297        return UNDEFINED;
298    }
299
300    private static void invokeSetterAccessor(final Accessors gs, final String name, final Object self, final Object value) {
301        final Object func = gs.setter;
302        if (func instanceof ScriptFunction) {
303            try {
304                getINVOKE_UA_SETTER().invokeExact(func, self, value);
305            } catch (final Error | RuntimeException t) {
306                throw t;
307            } catch (final Throwable t) {
308                throw new RuntimeException(t);
309            }
310        } else if (name != null) {
311            throw typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self));
312        }
313    }
314
315    private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) {
316        return MH.findStatic(LOOKUP, UserAccessorProperty.class, name, MH.type(rtype, types));
317    }
318
319}
320