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 26 package jdk.nashorn.internal.runtime; 27 import static jdk.nashorn.internal.lookup.Lookup.MH; 28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 30 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT; 31 import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.CALLSITE_PROGRAM_POINT_SHIFT; 32 33 import java.lang.invoke.MethodHandle; 34 import java.lang.invoke.MethodHandles; 35 import java.lang.invoke.MethodType; 36 import java.util.concurrent.Callable; 37 import jdk.nashorn.internal.lookup.Lookup; 38 import jdk.nashorn.internal.runtime.linker.Bootstrap; 39 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 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 */ 46 public 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 Accessors(final Object getter, final Object setter)54 Accessors(final Object getter, final Object setter) { 55 set(getter, setter); 56 } 57 set(final Object getter, final Object setter)58 final void set(final Object getter, final Object setter) { 59 this.getter = getter; 60 this.setter = setter; 61 } 62 63 @Override toString()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_OBJECT_GETTER = findOwnMH_S("invokeObjectGetter", Object.class, Accessors.class, MethodHandle.class, Object.class); 73 private final static MethodHandle INVOKE_INT_GETTER = findOwnMH_S("invokeIntGetter", int.class, Accessors.class, MethodHandle.class, int.class, Object.class); 74 private final static MethodHandle INVOKE_NUMBER_GETTER = findOwnMH_S("invokeNumberGetter", double.class, Accessors.class, MethodHandle.class, int.class, Object.class); 75 76 /** Setter method handle */ 77 private final static MethodHandle INVOKE_OBJECT_SETTER = findOwnMH_S("invokeObjectSetter", void.class, Accessors.class, MethodHandle.class, String.class, Object.class, Object.class); 78 private final static MethodHandle INVOKE_INT_SETTER = findOwnMH_S("invokeIntSetter", void.class, Accessors.class, MethodHandle.class, String.class, Object.class, int.class); 79 private final static MethodHandle INVOKE_NUMBER_SETTER = findOwnMH_S("invokeNumberSetter", void.class, Accessors.class, MethodHandle.class, String.class, Object.class, double.class); 80 81 private static final Object OBJECT_GETTER_INVOKER_KEY = new Object(); getObjectGetterInvoker()82 private static MethodHandle getObjectGetterInvoker() { 83 return Context.getGlobal().getDynamicInvoker(OBJECT_GETTER_INVOKER_KEY, new Callable<MethodHandle>() { 84 @Override 85 public MethodHandle call() throws Exception { 86 return getINVOKE_UA_GETTER(Object.class, INVALID_PROGRAM_POINT); 87 } 88 }); 89 } 90 91 static MethodHandle getINVOKE_UA_GETTER(final Class<?> returnType, final int programPoint) { 92 if (UnwarrantedOptimismException.isValid(programPoint)) { 93 final int flags = NashornCallSiteDescriptor.CALLSITE_OPTIMISTIC | programPoint << CALLSITE_PROGRAM_POINT_SHIFT; 94 return Bootstrap.createDynamicInvoker("dyn:call", flags, returnType, Object.class, Object.class); 95 } else { 96 return Bootstrap.createDynamicInvoker("dyn:call", Object.class, Object.class, Object.class); 97 } 98 } 99 100 private static final Object OBJECT_SETTER_INVOKER_KEY = new Object(); 101 private static MethodHandle getObjectSetterInvoker() { 102 return Context.getGlobal().getDynamicInvoker(OBJECT_SETTER_INVOKER_KEY, new Callable<MethodHandle>() { 103 @Override 104 public MethodHandle call() throws Exception { 105 return getINVOKE_UA_SETTER(Object.class); 106 } 107 }); 108 } 109 110 static MethodHandle getINVOKE_UA_SETTER(final Class<?> valueType) { 111 return Bootstrap.createDynamicInvoker("dyn:call", void.class, Object.class, Object.class, valueType); 112 } 113 114 /** 115 * Constructor 116 * 117 * @param key property key 118 * @param flags property flags 119 * @param slot spill slot 120 */ 121 UserAccessorProperty(final String key, final int flags, final int slot) { 122 super(key, flags, slot); 123 } 124 125 private UserAccessorProperty(final UserAccessorProperty property) { 126 super(property); 127 } 128 129 private UserAccessorProperty(final UserAccessorProperty property, final Class<?> newType) { 130 super(property, newType); 131 } 132 133 @Override 134 public Property copy() { 135 return new UserAccessorProperty(this); 136 } 137 138 @Override 139 public Property copy(final Class<?> newType) { 140 return new UserAccessorProperty(this, newType); 141 } 142 143 void setAccessors(final ScriptObject sobj, final PropertyMap map, final Accessors gs) { 144 try { 145 //invoke the getter and find out 146 super.getSetter(Object.class, map).invokeExact((Object)sobj, (Object)gs); 147 } catch (final Error | RuntimeException t) { 148 throw t; 149 } catch (final Throwable t) { 150 throw new RuntimeException(t); 151 } 152 } 153 154 //pick the getter setter out of the correct spill slot in sobj 155 Accessors getAccessors(final ScriptObject sobj) { 156 try { 157 //invoke the super getter with this spill slot 158 //get the getter setter from the correct spill slot 159 final Object gs = super.getGetter(Object.class).invokeExact((Object)sobj); 160 return (Accessors)gs; 161 } catch (final Error | RuntimeException t) { 162 throw t; 163 } catch (final Throwable t) { 164 throw new RuntimeException(t); 165 } 166 } 167 168 @Override 169 protected Class<?> getLocalType() { 170 return Object.class; 171 } 172 173 @Override 174 public boolean hasGetterFunction(final ScriptObject sobj) { 175 return getAccessors(sobj).getter != null; 176 } 177 178 @Override 179 public boolean hasSetterFunction(final ScriptObject sobj) { 180 return getAccessors(sobj).setter != null; 181 } 182 183 @Override 184 public int getIntValue(final ScriptObject self, final ScriptObject owner) { 185 return (int)getObjectValue(self, owner); 186 } 187 188 @Override 189 public double getDoubleValue(final ScriptObject self, final ScriptObject owner) { 190 return (double)getObjectValue(self, owner); 191 } 192 193 @Override 194 public Object getObjectValue(final ScriptObject self, final ScriptObject owner) { 195 try { 196 return invokeObjectGetter(getAccessors((owner != null) ? owner : self), getObjectGetterInvoker(), self); 197 } catch (final Error | RuntimeException t) { 198 throw t; 199 } catch (final Throwable t) { 200 throw new RuntimeException(t); 201 } 202 } 203 204 @Override 205 public void setValue(final ScriptObject self, final ScriptObject owner, final int value, final boolean strict) { 206 setValue(self, owner, (Object) value, strict); 207 } 208 209 @Override 210 public void setValue(final ScriptObject self, final ScriptObject owner, final double value, final boolean strict) { 211 setValue(self, owner, (Object) value, strict); 212 } 213 214 @Override 215 public void setValue(final ScriptObject self, final ScriptObject owner, final Object value, final boolean strict) { 216 try { 217 invokeObjectSetter(getAccessors((owner != null) ? owner : self), getObjectSetterInvoker(), strict ? getKey() : null, self, value); 218 } catch (final Error | RuntimeException t) { 219 throw t; 220 } catch (final Throwable t) { 221 throw new RuntimeException(t); 222 } 223 } 224 225 @Override 226 public MethodHandle getGetter(final Class<?> type) { 227 //this returns a getter on the format (Accessors, Object receiver) 228 return Lookup.filterReturnType(INVOKE_OBJECT_GETTER, type); 229 } 230 231 @Override 232 public MethodHandle getOptimisticGetter(final Class<?> type, final int programPoint) { 233 if (type == int.class) { 234 return INVOKE_INT_GETTER; 235 } else if (type == double.class) { 236 return INVOKE_NUMBER_GETTER; 237 } else { 238 assert type == Object.class; 239 return INVOKE_OBJECT_GETTER; 240 } 241 } 242 243 @Override 244 void initMethodHandles(final Class<?> structure) { 245 throw new UnsupportedOperationException(); 246 } 247 248 @Override 249 public ScriptFunction getGetterFunction(final ScriptObject sobj) { 250 final Object value = getAccessors(sobj).getter; 251 return (value instanceof ScriptFunction) ? (ScriptFunction)value : null; 252 } 253 254 @Override 255 public MethodHandle getSetter(final Class<?> type, final PropertyMap currentMap) { 256 if (type == int.class) { 257 return INVOKE_INT_SETTER; 258 } else if (type == double.class) { 259 return INVOKE_NUMBER_SETTER; 260 } else { 261 assert type == Object.class; 262 return INVOKE_OBJECT_SETTER; 263 } 264 } 265 266 @Override 267 public ScriptFunction getSetterFunction(final ScriptObject sobj) { 268 final Object value = getAccessors(sobj).setter; 269 return (value instanceof ScriptFunction) ? (ScriptFunction)value : null; 270 } 271 272 /** 273 * Get the getter for the {@code Accessors} object. 274 * This is the the super {@code Object} type getter with {@code Accessors} return type. 275 * 276 * @return The getter handle for the Accessors 277 */ 278 MethodHandle getAccessorsGetter() { 279 return super.getGetter(Object.class).asType(MethodType.methodType(Accessors.class, Object.class)); 280 } 281 282 // User defined getter and setter are always called by "dyn:call". Note that the user 283 // getter/setter may be inherited. If so, proto is bound during lookup. In either 284 // inherited or self case, slot is also bound during lookup. Actual ScriptFunction 285 // to be called is retrieved everytime and applied. 286 @SuppressWarnings("unused") 287 private static Object invokeObjectGetter(final Accessors gs, final MethodHandle invoker, final Object self) throws Throwable { 288 final Object func = gs.getter; 289 if (func instanceof ScriptFunction) { 290 return invoker.invokeExact(func, self); 291 } 292 293 return UNDEFINED; 294 } 295 296 @SuppressWarnings("unused") 297 private static int invokeIntGetter(final Accessors gs, final MethodHandle invoker, final int programPoint, final Object self) throws Throwable { 298 final Object func = gs.getter; 299 if (func instanceof ScriptFunction) { 300 return (int) invoker.invokeExact(func, self); 301 } 302 303 throw new UnwarrantedOptimismException(UNDEFINED, programPoint); 304 } 305 306 @SuppressWarnings("unused") 307 private static double invokeNumberGetter(final Accessors gs, final MethodHandle invoker, final int programPoint, final Object self) throws Throwable { 308 final Object func = gs.getter; 309 if (func instanceof ScriptFunction) { 310 return (double) invoker.invokeExact(func, self); 311 } 312 313 throw new UnwarrantedOptimismException(UNDEFINED, programPoint); 314 } 315 316 @SuppressWarnings("unused") 317 private static void invokeObjectSetter(final Accessors gs, final MethodHandle invoker, final String name, final Object self, final Object value) throws Throwable { 318 final Object func = gs.setter; 319 if (func instanceof ScriptFunction) { 320 invoker.invokeExact(func, self, value); 321 } else if (name != null) { 322 throw typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self)); 323 } 324 } 325 326 @SuppressWarnings("unused") 327 private static void invokeIntSetter(final Accessors gs, final MethodHandle invoker, final String name, final Object self, final int value) throws Throwable { 328 final Object func = gs.setter; 329 if (func instanceof ScriptFunction) { 330 invoker.invokeExact(func, self, value); 331 } else if (name != null) { 332 throw typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self)); 333 } 334 } 335 336 @SuppressWarnings("unused") 337 private static void invokeNumberSetter(final Accessors gs, final MethodHandle invoker, final String name, final Object self, final double value) throws Throwable { 338 final Object func = gs.setter; 339 if (func instanceof ScriptFunction) { 340 invoker.invokeExact(func, self, value); 341 } else if (name != null) { 342 throw typeError("property.has.no.setter", name, ScriptRuntime.safeToString(self)); 343 } 344 } 345 346 private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { 347 return MH.findStatic(LOOKUP, UserAccessorProperty.class, name, MH.type(rtype, types)); 348 } 349 350 } 351