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