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 28 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.ACCESSOR_TYPES; 29 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.DEBUG_FIELDS; 30 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.LOG; 31 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.OBJECT_FIELDS_ONLY; 32 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.PRIMITIVE_TYPE; 33 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.createGetter; 34 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.createGuardBoxedPrimitiveSetter; 35 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.createSetter; 36 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getAccessorType; 37 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getAccessorTypeIndex; 38 import static jdk.nashorn.internal.codegen.ObjectClassGenerator.getNumberOfAccessorTypes; 39 import static jdk.nashorn.internal.lookup.Lookup.MH; 40 import static jdk.nashorn.internal.lookup.MethodHandleFactory.stripName; 41 42 import java.lang.invoke.MethodHandle; 43 import java.lang.invoke.MethodHandles; 44 import java.lang.invoke.MethodType; 45 import jdk.nashorn.internal.codegen.ObjectClassGenerator; 46 import jdk.nashorn.internal.codegen.types.Type; 47 import jdk.nashorn.internal.lookup.Lookup; 48 import jdk.nashorn.internal.lookup.MethodHandleFactory; 49 50 /** 51 * An AccessorProperty is the most generic property type. An AccessorProperty is 52 * represented as fields in a ScriptObject class. 53 */ 54 public final class AccessorProperty extends Property { 55 private static final MethodHandles.Lookup lookup = MethodHandles.lookup(); 56 private static final MethodHandle REPLACE_MAP = findOwnMH("replaceMap", Object.class, Object.class, PropertyMap.class, String.class, Class.class, Class.class); 57 58 private static final int NOOF_TYPES = getNumberOfAccessorTypes(); 59 60 /** 61 * Properties in different maps for the same structure class will share their field getters and setters. This could 62 * be further extended to other method handles that are looked up in the AccessorProperty constructor, but right now 63 * these are the most frequently retrieved ones, and lookup of method handle natives only registers in the profiler 64 * for them. 65 */ 66 private static ClassValue<GettersSetters> GETTERS_SETTERS = new ClassValue<GettersSetters>() { 67 @Override 68 protected GettersSetters computeValue(Class<?> structure) { 69 return new GettersSetters(structure); 70 } 71 }; 72 73 /** Property getter cache */ 74 private MethodHandle[] getters = new MethodHandle[NOOF_TYPES]; 75 76 private static final MethodType[] ACCESSOR_GETTER_TYPES = new MethodType[NOOF_TYPES]; 77 private static final MethodType[] ACCESSOR_SETTER_TYPES = new MethodType[NOOF_TYPES]; 78 private static final MethodType ACCESSOR_GETTER_PRIMITIVE_TYPE; 79 private static final MethodType ACCESSOR_SETTER_PRIMITIVE_TYPE; 80 private static final MethodHandle SPILL_ELEMENT_GETTER; 81 private static final MethodHandle SPILL_ELEMENT_SETTER; 82 83 private static final int SPILL_CACHE_SIZE = 8; 84 private static final MethodHandle[] SPILL_ACCESSORS = new MethodHandle[SPILL_CACHE_SIZE * 2]; 85 86 static { 87 MethodType getterPrimitiveType = null; 88 MethodType setterPrimitiveType = null; 89 90 for (int i = 0; i < NOOF_TYPES; i++) { 91 final Type type = ACCESSOR_TYPES.get(i); 92 ACCESSOR_GETTER_TYPES[i] = MH.type(type.getTypeClass(), Object.class); 93 ACCESSOR_SETTER_TYPES[i] = MH.type(void.class, Object.class, type.getTypeClass()); 94 95 if (type == PRIMITIVE_TYPE) { 96 getterPrimitiveType = ACCESSOR_GETTER_TYPES[i]; 97 setterPrimitiveType = ACCESSOR_SETTER_TYPES[i]; 98 } 99 } 100 101 ACCESSOR_GETTER_PRIMITIVE_TYPE = getterPrimitiveType; 102 ACCESSOR_SETTER_PRIMITIVE_TYPE = setterPrimitiveType; 103 104 final MethodType spillGetterType = MethodType.methodType(Object[].class, Object.class); 105 final MethodHandle spillGetter = MH.asType(MH.getter(MethodHandles.lookup(), ScriptObject.class, "spill", Object[].class), spillGetterType); 106 SPILL_ELEMENT_GETTER = MH.filterArguments(MH.arrayElementGetter(Object[].class), 0, spillGetter); 107 SPILL_ELEMENT_SETTER = MH.filterArguments(MH.arrayElementSetter(Object[].class), 0, spillGetter); 108 } 109 110 /** 111 * Create a new accessor property. Factory method used by nasgen generated code. 112 * 113 * @param key {@link Property} key. 114 * @param propertyFlags {@link Property} flags. 115 * @param getter {@link Property} get accessor method. 116 * @param setter {@link Property} set accessor method. 117 * 118 * @return New {@link AccessorProperty} created. 119 */ 120 public static AccessorProperty create(final String key, final int propertyFlags, final MethodHandle getter, final MethodHandle setter) { 121 return new AccessorProperty(key, propertyFlags, -1, getter, setter); 122 } 123 124 /** Seed getter for the primitive version of this field (in -Dnashorn.fields.dual=true mode) */ 125 private MethodHandle primitiveGetter; 126 127 /** Seed setter for the primitive version of this field (in -Dnashorn.fields.dual=true mode) */ 128 private MethodHandle primitiveSetter; 129 130 /** Seed getter for the Object version of this field */ 131 private MethodHandle objectGetter; 132 133 /** Seed setter for the Object version of this field */ 134 private MethodHandle objectSetter; 135 136 /** 137 * Current type of this object, in object only mode, this is an Object.class. In dual-fields mode 138 * null means undefined, and primitive types are allowed. The reason a special type is used for 139 * undefined, is that are no bits left to represent it in primitive types 140 */ 141 private Class<?> currentType; 142 143 /** 144 * Delegate constructor. This is used when adding properties to the Global scope, which 145 * is necessary for outermost levels in a script (the ScriptObject is represented by 146 * a JO-prefixed ScriptObject class, but the properties need to be in the Global scope 147 * and are thus rebound with that as receiver 148 * 149 * @param property accessor property to rebind 150 * @param delegate delegate object to rebind receiver to 151 */ 152 AccessorProperty(final AccessorProperty property, final Object delegate) { 153 super(property); 154 155 this.primitiveGetter = bindTo(property.primitiveGetter, delegate); 156 this.primitiveSetter = bindTo(property.primitiveSetter, delegate); 157 this.objectGetter = bindTo(property.ensureObjectGetter(), delegate); 158 this.objectSetter = bindTo(property.ensureObjectSetter(), delegate); 159 160 setCurrentType(property.getCurrentType()); 161 } 162 163 /** 164 * Constructor for spill properties. Array getters and setters will be created on demand. 165 * 166 * @param key the property key 167 * @param flags the property flags 168 * @param slot spill slot 169 */ 170 public AccessorProperty(final String key, final int flags, final int slot) { 171 super(key, flags, slot); 172 assert (flags & IS_SPILL) == IS_SPILL; 173 174 setCurrentType(Object.class); 175 } 176 177 /** 178 * Constructor. Similar to the constructor with both primitive getters and setters, the difference 179 * here being that only one getter and setter (setter is optional for non writable fields) is given 180 * to the constructor, and the rest are created from those. Used e.g. by Nasgen classes 181 * 182 * @param key the property key 183 * @param flags the property flags 184 * @param slot the property field number or spill slot 185 * @param getter the property getter 186 * @param setter the property setter or null if non writable, non configurable 187 */ 188 AccessorProperty(final String key, final int flags, final int slot, final MethodHandle getter, final MethodHandle setter) { 189 super(key, flags, slot); 190 191 // we don't need to prep the setters these will never be invalidated as this is a nasgen 192 // or known type getter/setter. No invalidations will take place 193 194 final Class<?> getterType = getter.type().returnType(); 195 final Class<?> setterType = setter == null ? null : setter.type().parameterType(1); 196 197 assert setterType == null || setterType == getterType; 198 199 if (getterType.isPrimitive()) { 200 for (int i = 0; i < NOOF_TYPES; i++) { 201 getters[i] = MH.asType( 202 Lookup.filterReturnType( 203 getter, 204 getAccessorType(i).getTypeClass()), 205 ACCESSOR_GETTER_TYPES[i]); 206 } 207 } else { 208 objectGetter = getter.type() != Lookup.GET_OBJECT_TYPE ? MH.asType(getter, Lookup.GET_OBJECT_TYPE) : getter; 209 objectSetter = setter != null && setter.type() != Lookup.SET_OBJECT_TYPE ? MH.asType(setter, Lookup.SET_OBJECT_TYPE) : setter; 210 } 211 212 setCurrentType(getterType); 213 } 214 215 private static class GettersSetters { 216 final MethodHandle[] getters; 217 final MethodHandle[] setters; 218 219 public GettersSetters(Class<?> structure) { 220 final int fieldCount = ObjectClassGenerator.getFieldCount(structure); 221 getters = new MethodHandle[fieldCount]; 222 setters = new MethodHandle[fieldCount]; 223 for(int i = 0; i < fieldCount; ++i) { 224 final String fieldName = ObjectClassGenerator.getFieldName(i, Type.OBJECT); 225 getters[i] = MH.asType(MH.getter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.GET_OBJECT_TYPE); 226 setters[i] = MH.asType(MH.setter(lookup, structure, fieldName, Type.OBJECT.getTypeClass()), Lookup.SET_OBJECT_TYPE); 227 } 228 } 229 } 230 231 /** 232 * Constructor for dual field AccessorPropertys. 233 * 234 * @param key property key 235 * @param flags property flags 236 * @param structure structure for objects associated with this property 237 * @param slot property field number or spill slot 238 */ 239 public AccessorProperty(final String key, final int flags, final Class<?> structure, final int slot) { 240 super(key, flags, slot); 241 242 /* 243 * primitiveGetter and primitiveSetter are only used in dual fields mode. Setting them to null also 244 * works in dual field mode, it only means that the property never has a primitive 245 * representation. 246 */ 247 primitiveGetter = null; 248 primitiveSetter = null; 249 250 if (isParameter() && hasArguments()) { 251 final MethodHandle arguments = MH.getter(lookup, structure, "arguments", ScriptObject.class); 252 253 objectGetter = MH.asType(MH.insertArguments(MH.filterArguments(ScriptObject.GET_ARGUMENT.methodHandle(), 0, arguments), 1, slot), Lookup.GET_OBJECT_TYPE); 254 objectSetter = MH.asType(MH.insertArguments(MH.filterArguments(ScriptObject.SET_ARGUMENT.methodHandle(), 0, arguments), 1, slot), Lookup.SET_OBJECT_TYPE); 255 } else { 256 final GettersSetters gs = GETTERS_SETTERS.get(structure); 257 objectGetter = gs.getters[slot]; 258 objectSetter = gs.setters[slot]; 259 260 if (!OBJECT_FIELDS_ONLY) { 261 final String fieldNamePrimitive = ObjectClassGenerator.getFieldName(slot, PRIMITIVE_TYPE); 262 final Class<?> typeClass = PRIMITIVE_TYPE.getTypeClass(); 263 primitiveGetter = MH.asType(MH.getter(lookup, structure, fieldNamePrimitive, typeClass), ACCESSOR_GETTER_PRIMITIVE_TYPE); 264 primitiveSetter = MH.asType(MH.setter(lookup, structure, fieldNamePrimitive, typeClass), ACCESSOR_SETTER_PRIMITIVE_TYPE); 265 } 266 } 267 268 Class<?> initialType = null; 269 270 if (OBJECT_FIELDS_ONLY || isAlwaysObject()) { 271 initialType = Object.class; 272 } else if (!canBePrimitive()) { 273 info(key + " cannot be primitive"); 274 initialType = Object.class; 275 } else { 276 info(key + " CAN be primitive"); 277 if (!canBeUndefined()) { 278 info(key + " is always defined"); 279 initialType = int.class; //double works too for less type invalidation, but this requires experimentation, e.g. var x = 17; x += 2 will turn it into double now because of lack of range analysis 280 } 281 } 282 283 // is always object means "is never initialized to undefined, and always of object type 284 setCurrentType(initialType); 285 } 286 287 /** 288 * Copy constructor 289 * 290 * @param property source property 291 */ 292 protected AccessorProperty(final AccessorProperty property) { 293 super(property); 294 295 this.getters = property.getters; 296 this.primitiveGetter = property.primitiveGetter; 297 this.primitiveSetter = property.primitiveSetter; 298 this.objectGetter = property.objectGetter; 299 this.objectSetter = property.objectSetter; 300 301 setCurrentType(property.getCurrentType()); 302 } 303 304 private static MethodHandle bindTo(final MethodHandle mh, final Object receiver) { 305 if (mh == null) { 306 return null; 307 } 308 309 return MH.dropArguments(MH.bindTo(mh, receiver), 0, Object.class); 310 } 311 312 @Override 313 protected Property copy() { 314 return new AccessorProperty(this); 315 } 316 317 @Override 318 public void setObjectValue(final ScriptObject self, final ScriptObject owner, final Object value, final boolean strict) { 319 if (isSpill()) { 320 self.spill[getSlot()] = value; 321 } else { 322 try { 323 getSetter(Object.class, self.getMap()).invokeExact((Object)self, value); 324 } catch (final Error|RuntimeException e) { 325 throw e; 326 } catch (final Throwable e) { 327 throw new RuntimeException(e); 328 } 329 } 330 } 331 332 @Override 333 public Object getObjectValue(final ScriptObject self, final ScriptObject owner) { 334 if (isSpill()) { 335 return self.spill[getSlot()]; 336 } 337 338 try { 339 return getGetter(Object.class).invokeExact((Object)self); 340 } catch (final Error|RuntimeException e) { 341 throw e; 342 } catch (final Throwable e) { 343 throw new RuntimeException(e); 344 } 345 } 346 347 // Spill getters and setters are lazily initialized, see JDK-8011630 348 private MethodHandle ensureObjectGetter() { 349 if (isSpill() && objectGetter == null) { 350 objectGetter = getSpillGetter(); 351 } 352 return objectGetter; 353 } 354 355 private MethodHandle ensureObjectSetter() { 356 if (isSpill() && objectSetter == null) { 357 objectSetter = getSpillSetter(); 358 } 359 return objectSetter; 360 } 361 362 @Override 363 public MethodHandle getGetter(final Class<?> type) { 364 final int i = getAccessorTypeIndex(type); 365 ensureObjectGetter(); 366 367 if (getters[i] == null) { 368 getters[i] = debug( 369 createGetter(currentType, type, primitiveGetter, objectGetter), 370 currentType, type, "get"); 371 } 372 373 return getters[i]; 374 } 375 376 private Property getWiderProperty(final Class<?> type) { 377 final AccessorProperty newProperty = new AccessorProperty(this); 378 newProperty.invalidate(type); 379 return newProperty; 380 } 381 382 private PropertyMap getWiderMap(final PropertyMap oldMap, final Property newProperty) { 383 final PropertyMap newMap = oldMap.replaceProperty(this, newProperty); 384 assert oldMap.size() > 0; 385 assert newMap.size() == oldMap.size(); 386 return newMap; 387 } 388 389 // the final three arguments are for debug printout purposes only 390 @SuppressWarnings("unused") 391 private static Object replaceMap(final Object sobj, final PropertyMap newMap, final String key, final Class<?> oldType, final Class<?> newType) { 392 if (DEBUG_FIELDS) { 393 final PropertyMap oldMap = ((ScriptObject)sobj).getMap(); 394 info("Type change for '" + key + "' " + oldType + "=>" + newType); 395 finest("setting map " + sobj + " from " + Debug.id(oldMap) + " to " + Debug.id(newMap) + " " + oldMap + " => " + newMap); 396 } 397 ((ScriptObject)sobj).setMap(newMap); 398 return sobj; 399 } 400 401 private MethodHandle generateSetter(final Class<?> forType, final Class<?> type) { 402 ensureObjectSetter(); 403 MethodHandle mh = createSetter(forType, type, primitiveSetter, objectSetter); 404 mh = debug(mh, currentType, type, "set"); 405 return mh; 406 } 407 408 @Override 409 public MethodHandle getSetter(final Class<?> type, final PropertyMap currentMap) { 410 final int i = getAccessorTypeIndex(type); 411 final int ci = currentType == null ? -1 : getAccessorTypeIndex(currentType); 412 final Class<?> forType = currentType == null ? type : currentType; 413 414 //if we are asking for an object setter, but are still a primitive type, we might try to box it 415 MethodHandle mh; 416 417 if (needsInvalidator(i, ci)) { 418 final Property newProperty = getWiderProperty(type); 419 final PropertyMap newMap = getWiderMap(currentMap, newProperty); 420 final MethodHandle widerSetter = newProperty.getSetter(type, newMap); 421 final MethodHandle explodeTypeSetter = MH.filterArguments(widerSetter, 0, MH.insertArguments(REPLACE_MAP, 1, newMap, getKey(), currentType, type)); 422 if (currentType != null && currentType.isPrimitive() && type == Object.class) { 423 //might try a box check on this to avoid widening field to object storage 424 mh = createGuardBoxedPrimitiveSetter(currentType, generateSetter(currentType, currentType), explodeTypeSetter); 425 } else { 426 mh = explodeTypeSetter; 427 } 428 } else { 429 mh = generateSetter(forType, type); 430 } 431 432 return mh; 433 } 434 435 @Override 436 public boolean canChangeType() { 437 if (OBJECT_FIELDS_ONLY) { 438 return false; 439 } 440 return currentType != Object.class && (isConfigurable() || isWritable()); 441 } 442 443 private boolean needsInvalidator(final int ti, final int fti) { 444 return canChangeType() && ti > fti; 445 } 446 447 private void invalidate(final Class<?> newType) { 448 getters = new MethodHandle[NOOF_TYPES]; 449 setCurrentType(newType); 450 } 451 452 private MethodHandle getSpillGetter() { 453 final int slot = getSlot(); 454 MethodHandle getter = slot < SPILL_CACHE_SIZE ? SPILL_ACCESSORS[slot * 2] : null; 455 if (getter == null) { 456 getter = MH.insertArguments(SPILL_ELEMENT_GETTER, 1, slot); 457 if (slot < SPILL_CACHE_SIZE) { 458 SPILL_ACCESSORS[slot * 2 + 0] = getter; 459 } 460 } 461 return getter; 462 } 463 464 private MethodHandle getSpillSetter() { 465 final int slot = getSlot(); 466 MethodHandle setter = slot < SPILL_CACHE_SIZE ? SPILL_ACCESSORS[slot * 2 + 1] : null; 467 if (setter == null) { 468 setter = MH.insertArguments(SPILL_ELEMENT_SETTER, 1, slot); 469 if (slot < SPILL_CACHE_SIZE) { 470 SPILL_ACCESSORS[slot * 2 + 1] = setter; 471 } 472 } 473 return setter; 474 } 475 476 private static void finest(final String str) { 477 if (DEBUG_FIELDS) { 478 LOG.finest(str); 479 } 480 } 481 482 private static void info(final String str) { 483 if (DEBUG_FIELDS) { 484 LOG.info(str); 485 } 486 } 487 488 private MethodHandle debug(final MethodHandle mh, final Class<?> forType, final Class<?> type, final String tag) { 489 if (DEBUG_FIELDS) { 490 return MethodHandleFactory.addDebugPrintout( 491 LOG, 492 mh, 493 tag + " '" + getKey() + "' (property="+ Debug.id(this) + ", forType=" + stripName(forType) + ", type=" + stripName(type) + ')'); 494 } 495 return mh; 496 } 497 498 private void setCurrentType(final Class<?> currentType) { 499 this.currentType = currentType; 500 } 501 502 @Override 503 public Class<?> getCurrentType() { 504 return currentType; 505 } 506 507 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 508 return MH.findStatic(lookup, AccessorProperty.class, name, MH.type(rtype, types)); 509 } 510 511 }