ScriptObject.java revision 1746:9c51738fd0e3
1193323Sed/* 2193323Sed * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4193323Sed * 5193323Sed * This code is free software; you can redistribute it and/or modify it 6193323Sed * under the terms of the GNU General Public License version 2 only, as 7193323Sed * published by the Free Software Foundation. Oracle designates this 8193323Sed * particular file as subject to the "Classpath" exception as provided 9193323Sed * by Oracle in the LICENSE file that accompanied this code. 10193323Sed * 11193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT 12193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13193323Sed * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14193323Sed * version 2 for more details (a copy is included in the LICENSE file that 15193323Sed * accompanied this code). 16193323Sed * 17193323Sed * You should have received a copy of the GNU General Public License version 18200581Srdivacky * 2 along with this work; if not, write to the Free Software Foundation, 19193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20193323Sed * 21193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22193323Sed * or visit www.oracle.com if you need additional information or have any 23193323Sed * questions. 24193323Sed */ 25193323Sed 26193323Sedpackage jdk.nashorn.internal.runtime; 27193323Sed 28193323Sedimport static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; 29193323Sedimport static jdk.nashorn.internal.codegen.CompilerConstants.virtualCall; 30193323Sedimport static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup; 31193323Sedimport static jdk.nashorn.internal.lookup.Lookup.MH; 32193323Sedimport static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; 33193323Sedimport static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 34203954Srdivackyimport static jdk.nashorn.internal.runtime.JSType.UNDEFINED_DOUBLE; 35193323Sedimport static jdk.nashorn.internal.runtime.JSType.UNDEFINED_INT; 36200581Srdivackyimport static jdk.nashorn.internal.runtime.PropertyDescriptor.CONFIGURABLE; 37193323Sedimport static jdk.nashorn.internal.runtime.PropertyDescriptor.ENUMERABLE; 38198090Srdivackyimport static jdk.nashorn.internal.runtime.PropertyDescriptor.GET; 39198090Srdivackyimport static jdk.nashorn.internal.runtime.PropertyDescriptor.SET; 40193323Sedimport static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE; 41193323Sedimport static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE; 42193323Sedimport static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 43200581Srdivackyimport static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT; 44200581Srdivackyimport static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid; 45200581Srdivackyimport static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndex; 46200581Srdivackyimport static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex; 47200581Srdivackyimport static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.isScopeFlag; 48200581Srdivackyimport static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.isStrictFlag; 49200581Srdivackyimport static jdk.nashorn.internal.runtime.linker.NashornGuards.explicitInstanceOfCheck; 50200581Srdivacky 51200581Srdivackyimport java.lang.invoke.MethodHandle; 52200581Srdivackyimport java.lang.invoke.MethodHandles; 53200581Srdivackyimport java.lang.invoke.MethodType; 54200581Srdivackyimport java.lang.invoke.SwitchPoint; 55200581Srdivackyimport java.lang.reflect.Array; 56200581Srdivackyimport java.util.AbstractMap; 57200581Srdivackyimport java.util.ArrayList; 58200581Srdivackyimport java.util.Arrays; 59200581Srdivackyimport java.util.Collection; 60200581Srdivackyimport java.util.Collections; 61200581Srdivackyimport java.util.HashSet; 62193323Sedimport java.util.Iterator; 63198090Srdivackyimport java.util.LinkedHashSet; 64198090Srdivackyimport java.util.List; 65193323Sedimport java.util.Map; 66200581Srdivackyimport java.util.Set; 67200581Srdivackyimport java.util.concurrent.atomic.LongAdder; 68193323Sedimport jdk.dynalink.CallSiteDescriptor; 69193323Sedimport jdk.dynalink.NamedOperation; 70193323Sedimport jdk.dynalink.StandardOperation; 71193323Sedimport jdk.dynalink.linker.GuardedInvocation; 72193323Sedimport jdk.dynalink.linker.LinkRequest; 73193323Sedimport jdk.nashorn.internal.codegen.CompilerConstants.Call; 74193323Sedimport jdk.nashorn.internal.codegen.ObjectClassGenerator; 75193323Sedimport jdk.nashorn.internal.codegen.types.Type; 76193323Sedimport jdk.nashorn.internal.lookup.Lookup; 77193323Sedimport jdk.nashorn.internal.objects.AccessorPropertyDescriptor; 78193323Sedimport jdk.nashorn.internal.objects.DataPropertyDescriptor; 79193323Sedimport jdk.nashorn.internal.objects.Global; 80193323Sedimport jdk.nashorn.internal.objects.NativeArray; 81193323Sedimport jdk.nashorn.internal.runtime.arrays.ArrayData; 82226633Sdimimport jdk.nashorn.internal.runtime.arrays.ArrayIndex; 83208599Srdivackyimport jdk.nashorn.internal.runtime.linker.Bootstrap; 84193323Sedimport jdk.nashorn.internal.runtime.linker.LinkerCallSite; 85199481Srdivackyimport jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 86199481Srdivackyimport jdk.nashorn.internal.runtime.linker.NashornGuards; 87199481Srdivacky 88199481Srdivacky/** 89199481Srdivacky * Base class for generic JavaScript objects. 90199481Srdivacky * <p> 91199481Srdivacky * Notes: 92199481Srdivacky * <ul> 93193323Sed * <li>The map is used to identify properties in the object.</li> 94193323Sed * <li>If the map is modified then it must be cloned and replaced. This notifies 95193323Sed * any code that made assumptions about the object that things have changed. 96193323Sed * Ex. CallSites that have been validated must check to see if the map has 97193323Sed * changed (or a map from a different object type) and hence relink the method 98193323Sed * to call.</li> 99193323Sed * <li>Modifications of the map include adding/deleting attributes or changing a 100193323Sed * function field value.</li> 101193323Sed * </ul> 102193323Sed */ 103193323Sed 104193323Sedpublic abstract class ScriptObject implements PropertyAccess, Cloneable { 105193323Sed /** __proto__ special property name inside object literals. ES6 draft. */ 106193323Sed public static final String PROTO_PROPERTY_NAME = "__proto__"; 107193323Sed 108193323Sed /** Search fall back routine name for "no such method" */ 109193323Sed public static final String NO_SUCH_METHOD_NAME = "__noSuchMethod__"; 110207618Srdivacky 111193323Sed /** Search fall back routine name for "no such property" */ 112193323Sed public static final String NO_SUCH_PROPERTY_NAME = "__noSuchProperty__"; 113193323Sed 114193323Sed /** Per ScriptObject flag - is this an array object? */ 115200581Srdivacky public static final int IS_ARRAY = 1 << 0; 116200581Srdivacky 117193323Sed /** Per ScriptObject flag - is this an arguments object? */ 118193323Sed public static final int IS_ARGUMENTS = 1 << 1; 119193323Sed 120193323Sed /** Is length property not-writable? */ 121193323Sed public static final int IS_LENGTH_NOT_WRITABLE = 1 << 2; 122198090Srdivacky 123198090Srdivacky /** Is this a builtin object? */ 124193323Sed public static final int IS_BUILTIN = 1 << 3; 125198090Srdivacky 126198090Srdivacky /** 127198090Srdivacky * Spill growth rate - by how many elements does {@link ScriptObject#primitiveSpill} and 128198090Srdivacky * {@link ScriptObject#objectSpill} when full 129198090Srdivacky */ 130198090Srdivacky public static final int SPILL_RATE = 8; 131198090Srdivacky 132198090Srdivacky /** Map to property information and accessor functions. Ordered by insertion. */ 133198090Srdivacky private PropertyMap map; 134198090Srdivacky 135198090Srdivacky /** objects proto. */ 136198090Srdivacky private ScriptObject proto; 137198090Srdivacky 138198090Srdivacky /** Object flags. */ 139198090Srdivacky private int flags; 140198090Srdivacky 141193323Sed /** Area for primitive properties added to object after instantiation, see {@link AccessorProperty} */ 142199481Srdivacky protected long[] primitiveSpill; 143199481Srdivacky 144199481Srdivacky /** Area for reference properties added to object after instantiation, see {@link AccessorProperty} */ 145199481Srdivacky protected Object[] objectSpill; 146199481Srdivacky 147193323Sed /** Indexed array data. */ 148193323Sed private ArrayData arrayData; 149193323Sed 150193323Sed /** Method handle to retrieve prototype of this object */ 151193323Sed public static final MethodHandle GETPROTO = findOwnMH_V("getProto", ScriptObject.class); 152193323Sed 153199481Srdivacky static final MethodHandle MEGAMORPHIC_GET = findOwnMH_V("megamorphicGet", Object.class, String.class, boolean.class, boolean.class); 154199481Srdivacky static final MethodHandle GLOBALFILTER = findOwnMH_S("globalFilter", Object.class, Object.class); 155199481Srdivacky static final MethodHandle DECLARE_AND_SET = findOwnMH_V("declareAndSet", void.class, String.class, Object.class); 156199481Srdivacky 157199481Srdivacky private static final MethodHandle TRUNCATINGFILTER = findOwnMH_S("truncatingFilter", Object[].class, int.class, Object[].class); 158199481Srdivacky private static final MethodHandle KNOWNFUNCPROPGUARDSELF = findOwnMH_S("knownFunctionPropertyGuardSelf", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, ScriptFunction.class); 159193323Sed private static final MethodHandle KNOWNFUNCPROPGUARDPROTO = findOwnMH_S("knownFunctionPropertyGuardProto", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, int.class, ScriptFunction.class); 160193323Sed 161193323Sed private static final ArrayList<MethodHandle> PROTO_FILTERS = new ArrayList<>(); 162193323Sed 163193323Sed /** Method handle for getting the array data */ 164193323Sed public static final Call GET_ARRAY = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArray", ArrayData.class); 165200581Srdivacky 166200581Srdivacky /** Method handle for getting a function argument at a given index. Used from MapCreator */ 167200581Srdivacky public static final Call GET_ARGUMENT = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArgument", Object.class, int.class); 168200581Srdivacky 169200581Srdivacky /** Method handle for setting a function argument at a given index. Used from MapCreator */ 170200581Srdivacky public static final Call SET_ARGUMENT = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setArgument", void.class, int.class, Object.class); 171200581Srdivacky 172200581Srdivacky /** Method handle for getting the proto of a ScriptObject */ 173200581Srdivacky public static final Call GET_PROTO = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class); 174223017Sdim 175223017Sdim /** Method handle for getting the proto of a ScriptObject */ 176223017Sdim public static final Call GET_PROTO_DEPTH = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class, int.class); 177193323Sed 178193323Sed /** Method handle for setting the proto of a ScriptObject */ 179207618Srdivacky public static final Call SET_GLOBAL_OBJECT_PROTO = staticCallNoLookup(ScriptObject.class, "setGlobalObjectProto", void.class, ScriptObject.class); 180207618Srdivacky 181193323Sed /** Method handle for setting the proto of a ScriptObject after checking argument */ 182193323Sed public static final Call SET_PROTO_FROM_LITERAL = virtualCallNoLookup(ScriptObject.class, "setProtoFromLiteral", void.class, Object.class); 183193323Sed 184193323Sed /** Method handle for setting the user accessors of a ScriptObject */ 185193323Sed //TODO fastpath this 186207618Srdivacky public static final Call SET_USER_ACCESSORS = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class); 187193323Sed 188200581Srdivacky static final MethodHandle[] SET_SLOW = new MethodHandle[] { 189193323Sed findOwnMH_V("set", void.class, Object.class, int.class, int.class), 190193323Sed findOwnMH_V("set", void.class, Object.class, double.class, int.class), 191193323Sed findOwnMH_V("set", void.class, Object.class, Object.class, int.class) 192200581Srdivacky }; 193200581Srdivacky 194193323Sed /** Method handle to reset the map of this ScriptObject */ 195198090Srdivacky public static final Call SET_MAP = virtualCallNoLookup(ScriptObject.class, "setMap", void.class, PropertyMap.class); 196193323Sed 197193323Sed static final MethodHandle CAS_MAP = findOwnMH_V("compareAndSetMap", boolean.class, PropertyMap.class, PropertyMap.class); 198193323Sed static final MethodHandle EXTENSION_CHECK = findOwnMH_V("extensionCheck", boolean.class, boolean.class, String.class); 199193323Sed static final MethodHandle ENSURE_SPILL_SIZE = findOwnMH_V("ensureSpillSize", Object.class, int.class); 200193323Sed 201198090Srdivacky /** 202198090Srdivacky * Constructor 203198090Srdivacky */ 204198090Srdivacky public ScriptObject() { 205198090Srdivacky this(null); 206198090Srdivacky } 207198090Srdivacky 208198090Srdivacky /** 209198090Srdivacky * Constructor 210198090Srdivacky * 211198090Srdivacky * @param map {@link PropertyMap} used to create the initial object 212198090Srdivacky */ 213198090Srdivacky public ScriptObject(final PropertyMap map) { 214198090Srdivacky if (Context.DEBUG) { 215198090Srdivacky ScriptObject.count.increment(); 216198090Srdivacky } 217198090Srdivacky this.arrayData = ArrayData.EMPTY_ARRAY; 218198090Srdivacky this.setMap(map == null ? PropertyMap.newMap() : map); 219198090Srdivacky } 220198090Srdivacky 221198090Srdivacky /** 222198090Srdivacky * Constructor that directly sets the prototype to {@code proto} and property map to 223198090Srdivacky * {@code map} without invalidating the map as calling {@link #setProto(ScriptObject)} 224198090Srdivacky * would do. This should only be used for objects that are always constructed with the 225198090Srdivacky * same combination of prototype and property map. 226198090Srdivacky * 227198090Srdivacky * @param proto the prototype object 228198090Srdivacky * @param map initial {@link PropertyMap} 229198090Srdivacky */ 230198090Srdivacky protected ScriptObject(final ScriptObject proto, final PropertyMap map) { 231198090Srdivacky this(map); 232198090Srdivacky this.proto = proto; 233198090Srdivacky } 234198090Srdivacky 235198090Srdivacky /** 236198090Srdivacky * Constructor used to instantiate spill properties directly. Used from 237198090Srdivacky * SpillObjectCreator. 238198090Srdivacky * 239193323Sed * @param map property maps 240193323Sed * @param primitiveSpill primitive spills 241193323Sed * @param objectSpill reference spills 242193323Sed */ 243193323Sed public ScriptObject(final PropertyMap map, final long[] primitiveSpill, final Object[] objectSpill) { 244198090Srdivacky this(map); 245198090Srdivacky this.primitiveSpill = primitiveSpill; 246198090Srdivacky this.objectSpill = objectSpill; 247198090Srdivacky assert primitiveSpill == null || primitiveSpill.length == objectSpill.length : " primitive spill pool size is not the same length as object spill pool size"; 248198090Srdivacky } 249198090Srdivacky 250198090Srdivacky /** 251198090Srdivacky * Check whether this is a global object 252207618Srdivacky * @return true if global 253207618Srdivacky */ 254198090Srdivacky protected boolean isGlobal() { 255198090Srdivacky return false; 256193323Sed } 257198090Srdivacky 258193323Sed private static int alignUp(final int size, final int alignment) { 259193323Sed return size + alignment - 1 & ~(alignment - 1); 260198090Srdivacky } 261200581Srdivacky 262200581Srdivacky /** 263200581Srdivacky * Given a number of properties, return the aligned to SPILL_RATE 264200581Srdivacky * buffer size required for the smallest spill pool needed to 265207618Srdivacky * house them 266200581Srdivacky * @param nProperties number of properties 267200581Srdivacky * @return property buffer length, a multiple of SPILL_RATE 268193323Sed */ 269193323Sed public static int spillAllocationLength(final int nProperties) { 270193323Sed return alignUp(nProperties, SPILL_RATE); 271198090Srdivacky } 272198090Srdivacky 273198090Srdivacky /** 274203954Srdivacky * Copy all properties from the source object with their receiver bound to the source. 275198090Srdivacky * This function was known as mergeMap 276210299Sed * 277198090Srdivacky * @param source The source object to copy from. 278198090Srdivacky */ 279207618Srdivacky public void addBoundProperties(final ScriptObject source) { 280203954Srdivacky addBoundProperties(source, source.getMap().getProperties()); 281203954Srdivacky } 282198090Srdivacky 283198090Srdivacky /** 284193323Sed * Copy all properties from the array with their receiver bound to the source. 285198090Srdivacky * 286193323Sed * @param source The source object to copy from. 287193323Sed * @param properties The array of properties to copy. 288198090Srdivacky */ 289210299Sed public void addBoundProperties(final ScriptObject source, final Property[] properties) { 290200581Srdivacky PropertyMap newMap = this.getMap(); 291207618Srdivacky final boolean extensible = newMap.isExtensible(); 292200581Srdivacky 293193323Sed for (final Property property : properties) { 294193323Sed newMap = addBoundProperty(newMap, source, property, extensible); 295193323Sed } 296193323Sed 297193323Sed this.setMap(newMap); 298193323Sed } 299193323Sed 300198090Srdivacky /** 301198090Srdivacky * Add a bound property from {@code source}, using the interim property map {@code propMap}, and return the 302198090Srdivacky * new interim property map. 303198090Srdivacky * 304198090Srdivacky * @param propMap the property map 305198090Srdivacky * @param source the source object 306198090Srdivacky * @param property the property to be added 307198090Srdivacky * @param extensible whether the current object is extensible or not 308207618Srdivacky * @return the new property map 309207618Srdivacky */ 310193323Sed protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final Property property, final boolean extensible) { 311193323Sed PropertyMap newMap = propMap; 312193323Sed final Object key = property.getKey(); 313193323Sed final Property oldProp = newMap.findProperty(key); 314193323Sed if (oldProp == null) { 315193323Sed if (! extensible) { 316223017Sdim throw typeError("object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 317223017Sdim } 318198090Srdivacky 319193323Sed if (property instanceof UserAccessorProperty) { 320193323Sed // Note: we copy accessor functions to this object which is semantically different from binding. 321193323Sed final UserAccessorProperty prop = this.newUserAccessors(key, property.getFlags(), property.getGetterFunction(source), property.getSetterFunction(source)); 322193323Sed newMap = newMap.addPropertyNoHistory(prop); 323193323Sed } else { 324193323Sed newMap = newMap.addPropertyBind((AccessorProperty)property, source); 325193323Sed } 326198090Srdivacky } else { 327198090Srdivacky // See ECMA section 10.5 Declaration Binding Instantiation 328219077Sdim // step 5 processing each function declaration. 329198090Srdivacky if (property.isFunctionDeclaration() && !oldProp.isConfigurable()) { 330198090Srdivacky if (oldProp instanceof UserAccessorProperty || 331198090Srdivacky !(oldProp.isWritable() && oldProp.isEnumerable())) { 332198090Srdivacky throw typeError("cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 333198090Srdivacky } 334198090Srdivacky } 335198090Srdivacky } 336193323Sed return newMap; 337193323Sed } 338193323Sed 339193323Sed /** 340198090Srdivacky * Copy all properties from the array with their receiver bound to the source. 341193323Sed * 342193323Sed * @param source The source object to copy from. 343193323Sed * @param properties The collection of accessor properties to copy. 344193323Sed */ 345193323Sed public void addBoundProperties(final Object source, final AccessorProperty[] properties) { 346193323Sed PropertyMap newMap = this.getMap(); 347193323Sed final boolean extensible = newMap.isExtensible(); 348193323Sed 349193323Sed for (final AccessorProperty property : properties) { 350193323Sed final Object key = property.getKey(); 351193323Sed 352193323Sed if (newMap.findProperty(key) == null) { 353193323Sed if (! extensible) { 354193323Sed throw typeError("object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 355198090Srdivacky } 356193323Sed newMap = newMap.addPropertyBind(property, source); 357193323Sed } 358193323Sed } 359193323Sed 360193323Sed this.setMap(newMap); 361193323Sed } 362193323Sed 363198090Srdivacky /** 364218893Sdim * Bind the method handle to the specified receiver, while preserving its original type (it will just ignore the 365193323Sed * first argument in lieu of the bound argument). 366193323Sed * @param methodHandle Method handle to bind to. 367193323Sed * @param receiver Object to bind. 368210299Sed * @return Bound method handle. 369193323Sed */ 370193323Sed static MethodHandle bindTo(final MethodHandle methodHandle, final Object receiver) { 371193323Sed return MH.dropArguments(MH.bindTo(methodHandle, receiver), 0, methodHandle.type().parameterType(0)); 372193323Sed } 373198090Srdivacky 374218893Sdim /** 375203954Srdivacky * Return a property iterator. 376193323Sed * @return Property iterator. 377193323Sed */ 378193323Sed public Iterator<String> propertyIterator() { 379198090Srdivacky return new KeyIterator(this); 380193323Sed } 381193323Sed 382198090Srdivacky /** 383198090Srdivacky * Return a property value iterator. 384198090Srdivacky * @return Property value iterator. 385198090Srdivacky */ 386210299Sed public Iterator<Object> valueIterator() { 387207618Srdivacky return new ValueIterator(this); 388198090Srdivacky } 389193323Sed 390193323Sed /** 391193323Sed * ECMA 8.10.1 IsAccessorDescriptor ( Desc ) 392200581Srdivacky * @return true if this has a {@link AccessorPropertyDescriptor} with a getter or a setter 393200581Srdivacky */ 394207618Srdivacky public final boolean isAccessorDescriptor() { 395200581Srdivacky return has(GET) || has(SET); 396200581Srdivacky } 397200581Srdivacky 398193323Sed /** 399223017Sdim * ECMA 8.10.2 IsDataDescriptor ( Desc ) 400223017Sdim * @return true if this has a {@link DataPropertyDescriptor}, i.e. the object has a property value and is writable 401193323Sed */ 402198090Srdivacky public final boolean isDataDescriptor() { 403198090Srdivacky return has(VALUE) || has(WRITABLE); 404193323Sed } 405193323Sed 406193323Sed /** 407193323Sed * ECMA 8.10.5 ToPropertyDescriptor ( Obj ) 408193323Sed * 409193323Sed * @return property descriptor 410193323Sed */ 411193323Sed public final PropertyDescriptor toPropertyDescriptor() { 412193323Sed final Global global = Context.getGlobal(); 413193323Sed 414193323Sed final PropertyDescriptor desc; 415193323Sed if (isDataDescriptor()) { 416193323Sed if (has(SET) || has(GET)) { 417193323Sed throw typeError(global, "inconsistent.property.descriptor"); 418193323Sed } 419193323Sed 420193323Sed desc = global.newDataDescriptor(UNDEFINED, false, false, false); 421210299Sed } else if (isAccessorDescriptor()) { 422193323Sed if (has(VALUE) || has(WRITABLE)) { 423193323Sed throw typeError(global, "inconsistent.property.descriptor"); 424193323Sed } 425193323Sed 426193323Sed desc = global.newAccessorDescriptor(UNDEFINED, UNDEFINED, false, false); 427193323Sed } else { 428200581Srdivacky desc = global.newGenericDescriptor(false, false); 429200581Srdivacky } 430200581Srdivacky 431193323Sed return desc.fillFrom(this); 432200581Srdivacky } 433193323Sed 434193323Sed /** 435200581Srdivacky * ECMA 8.10.5 ToPropertyDescriptor ( Obj ) 436193323Sed * 437193323Sed * @param global global scope object 438193323Sed * @param obj object to create property descriptor from 439193323Sed * 440193323Sed * @return property descriptor 441198090Srdivacky */ 442198090Srdivacky public static PropertyDescriptor toPropertyDescriptor(final Global global, final Object obj) { 443198090Srdivacky if (obj instanceof ScriptObject) { 444198090Srdivacky return ((ScriptObject)obj).toPropertyDescriptor(); 445198090Srdivacky } 446198090Srdivacky 447210299Sed throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj)); 448198090Srdivacky } 449198090Srdivacky 450207618Srdivacky /** 451193323Sed * ECMA 8.12.1 [[GetOwnProperty]] (P) 452193323Sed * 453223017Sdim * @param key property key 454223017Sdim * 455193323Sed * @return Returns the Property Descriptor of the named own property of this 456198090Srdivacky * object, or undefined if absent. 457193323Sed */ 458193323Sed public Object getOwnPropertyDescriptor(final Object key) { 459193323Sed final Property property = getMap().findProperty(key); 460193323Sed 461193323Sed final Global global = Context.getGlobal(); 462193323Sed 463193323Sed if (property != null) { 464193323Sed final ScriptFunction get = property.getGetterFunction(this); 465193323Sed final ScriptFunction set = property.getSetterFunction(this); 466193323Sed 467193323Sed final boolean configurable = property.isConfigurable(); 468193323Sed final boolean enumerable = property.isEnumerable(); 469193323Sed final boolean writable = property.isWritable(); 470193323Sed 471193323Sed if (property.isAccessorProperty()) { 472210299Sed return global.newAccessorDescriptor( 473193323Sed get != null ? 474193323Sed get : 475193323Sed UNDEFINED, 476198090Srdivacky set != null ? 477193323Sed set : 478193323Sed UNDEFINED, 479193323Sed configurable, 480193323Sed enumerable); 481193323Sed } 482193323Sed 483193323Sed return global.newDataDescriptor(getWithProperty(property), configurable, enumerable, writable); 484193323Sed } 485193323Sed 486193323Sed final int index = getArrayIndex(key); 487193323Sed final ArrayData array = getArray(); 488193323Sed 489193323Sed if (array.has(index)) { 490193323Sed return array.getDescriptor(global, index); 491193323Sed } 492193323Sed 493193323Sed return UNDEFINED; 494193323Sed } 495193323Sed 496193323Sed /** 497193323Sed * ECMA 8.12.2 [[GetProperty]] (P) 498193323Sed * 499193323Sed * @param key property key 500193323Sed * 501193323Sed * @return Returns the fully populated Property Descriptor of the named property 502193323Sed * of this object, or undefined if absent. 503193323Sed */ 504193323Sed public Object getPropertyDescriptor(final String key) { 505218893Sdim final Object res = getOwnPropertyDescriptor(key); 506193323Sed 507193323Sed if (res != UNDEFINED) { 508193323Sed return res; 509193323Sed } else if (getProto() != null) { 510193323Sed return getProto().getOwnPropertyDescriptor(key); 511193323Sed } else { 512193323Sed return UNDEFINED; 513193323Sed } 514193323Sed } 515193323Sed 516193323Sed /** 517221345Sdim * Invalidate any existing global constant method handles that may exist for {@code key}. 518193323Sed * @param key the property name 519193323Sed */ 520193323Sed protected void invalidateGlobalConstant(final Object key) { 521193323Sed final GlobalConstants globalConstants = getGlobalConstants(); 522193323Sed if (globalConstants != null) { 523193323Sed globalConstants.delete(key); 524193323Sed } 525193323Sed } 526193323Sed 527193323Sed /** 528193323Sed * ECMA 8.12.9 [[DefineOwnProperty]] (P, Desc, Throw) 529210299Sed * 530193323Sed * @param key the property key 531193323Sed * @param propertyDesc the property descriptor 532193323Sed * @param reject is the property extensible - true means new definitions are rejected 533193323Sed * 534218893Sdim * @return true if property was successfully defined 535193323Sed */ 536193323Sed public boolean defineOwnProperty(final Object key, final Object propertyDesc, final boolean reject) { 537193323Sed final Global global = Context.getGlobal(); 538193323Sed final PropertyDescriptor desc = toPropertyDescriptor(global, propertyDesc); 539193323Sed final Object current = getOwnPropertyDescriptor(key); 540193323Sed 541193323Sed invalidateGlobalConstant(key); 542193323Sed 543193323Sed if (current == UNDEFINED) { 544193323Sed if (isExtensible()) { 545193323Sed // add a new own property 546193323Sed addOwnProperty(key, desc); 547193323Sed return true; 548193323Sed } 549193323Sed // new property added to non-extensible object 550193323Sed if (reject) { 551193323Sed throw typeError(global, "object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 552193323Sed } 553193323Sed return false; 554193323Sed } 555193323Sed 556193323Sed // modifying an existing property 557193323Sed final PropertyDescriptor currentDesc = (PropertyDescriptor)current; 558193323Sed final PropertyDescriptor newDesc = desc; 559193323Sed 560198090Srdivacky if (newDesc.type() == PropertyDescriptor.GENERIC && !newDesc.has(CONFIGURABLE) && !newDesc.has(ENUMERABLE)) { 561198090Srdivacky // every descriptor field is absent 562193323Sed return true; 563193323Sed } 564198090Srdivacky 565198090Srdivacky if (newDesc.hasAndEquals(currentDesc)) { 566198090Srdivacky // every descriptor field of the new is same as the current 567198090Srdivacky return true; 568193323Sed } 569198090Srdivacky 570198090Srdivacky if (!currentDesc.isConfigurable()) { 571198090Srdivacky if (newDesc.has(CONFIGURABLE) && newDesc.isConfigurable()) { 572207618Srdivacky // not configurable can not be made configurable 573193323Sed if (reject) { 574193323Sed throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 575193323Sed } 576223017Sdim return false; 577223017Sdim } 578193323Sed 579198090Srdivacky if (newDesc.has(ENUMERABLE) && 580193323Sed currentDesc.isEnumerable() != newDesc.isEnumerable()) { 581193323Sed // cannot make non-enumerable as enumerable or vice-versa 582193323Sed if (reject) { 583193323Sed throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 584193323Sed } 585193323Sed return false; 586198090Srdivacky } 587193323Sed } 588193323Sed 589198090Srdivacky int propFlags = Property.mergeFlags(currentDesc, newDesc); 590193323Sed Property property = getMap().findProperty(key); 591193323Sed 592193323Sed if (currentDesc.type() == PropertyDescriptor.DATA && 593207618Srdivacky (newDesc.type() == PropertyDescriptor.DATA || 594193323Sed newDesc.type() == PropertyDescriptor.GENERIC)) { 595193323Sed if (!currentDesc.isConfigurable() && !currentDesc.isWritable()) { 596198090Srdivacky if (newDesc.has(WRITABLE) && newDesc.isWritable() || 597193323Sed newDesc.has(VALUE) && !ScriptRuntime.sameValue(currentDesc.getValue(), newDesc.getValue())) { 598193323Sed if (reject) { 599200581Srdivacky throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 600193323Sed } 601200581Srdivacky return false; 602200581Srdivacky } 603200581Srdivacky } 604200581Srdivacky 605200581Srdivacky final boolean newValue = newDesc.has(VALUE); 606200581Srdivacky final Object value = newValue ? newDesc.getValue() : currentDesc.getValue(); 607200581Srdivacky 608200581Srdivacky if (newValue && property != null) { 609200581Srdivacky // Temporarily clear flags. 610200581Srdivacky property = modifyOwnProperty(property, 0); 611200581Srdivacky set(key, value, 0); 612200581Srdivacky //this might change the map if we change types of the property 613200581Srdivacky //hence we need to read it again. note that we should probably 614193323Sed //have the setter return the new property throughout and in 615193323Sed //general respect Property return values from modify and add 616193323Sed //functions - which we don't seem to do at all here :-( 617193323Sed //There is already a bug filed to generify PropertyAccess so we 618193323Sed //can have the setter return e.g. a Property 619193323Sed property = getMap().findProperty(key); 620193323Sed } 621193323Sed 622193323Sed if (property == null) { 623193323Sed // promoting an arrayData value to actual property 624193323Sed addOwnProperty(key, propFlags, value); 625193323Sed checkIntegerKey(key); 626193323Sed } else { 627193323Sed // Now set the new flags 628193323Sed modifyOwnProperty(property, propFlags); 629193323Sed } 630193323Sed } else if (currentDesc.type() == PropertyDescriptor.ACCESSOR && 631193323Sed (newDesc.type() == PropertyDescriptor.ACCESSOR || 632193323Sed newDesc.type() == PropertyDescriptor.GENERIC)) { 633193323Sed if (!currentDesc.isConfigurable()) { 634193323Sed if (newDesc.has(PropertyDescriptor.GET) && !ScriptRuntime.sameValue(currentDesc.getGetter(), newDesc.getGetter()) || 635193323Sed newDesc.has(PropertyDescriptor.SET) && !ScriptRuntime.sameValue(currentDesc.getSetter(), newDesc.getSetter())) { 636207618Srdivacky if (reject) { 637207618Srdivacky throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 638193323Sed } 639193323Sed return false; 640193323Sed } 641193323Sed } 642210299Sed // New set the new features. 643210299Sed modifyOwnProperty(property, propFlags, 644193323Sed newDesc.has(GET) ? newDesc.getGetter() : currentDesc.getGetter(), 645193323Sed newDesc.has(SET) ? newDesc.getSetter() : currentDesc.getSetter()); 646193323Sed } else { 647193323Sed // changing descriptor type 648193323Sed if (!currentDesc.isConfigurable()) { 649207618Srdivacky // not configurable can not be made configurable 650193323Sed if (reject) { 651193323Sed throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 652193323Sed } 653193323Sed return false; 654193323Sed } 655193323Sed 656193323Sed propFlags = 0; 657207618Srdivacky 658207618Srdivacky // Preserve only configurable and enumerable from current desc 659207618Srdivacky // if those are not overridden in the new property descriptor. 660207618Srdivacky boolean value = newDesc.has(CONFIGURABLE) ? newDesc.isConfigurable() : currentDesc.isConfigurable(); 661207618Srdivacky if (!value) { 662207618Srdivacky propFlags |= Property.NOT_CONFIGURABLE; 663207618Srdivacky } 664207618Srdivacky value = newDesc.has(ENUMERABLE)? newDesc.isEnumerable() : currentDesc.isEnumerable(); 665207618Srdivacky if (!value) { 666198396Srdivacky propFlags |= Property.NOT_ENUMERABLE; 667193323Sed } 668193323Sed 669193323Sed final int type = newDesc.type(); 670193323Sed if (type == PropertyDescriptor.DATA) { 671193323Sed // get writable from the new descriptor 672193323Sed value = newDesc.has(WRITABLE) && newDesc.isWritable(); 673198396Srdivacky if (!value) { 674193323Sed propFlags |= Property.NOT_WRITABLE; 675198090Srdivacky } 676193323Sed 677198396Srdivacky // delete the old property 678202375Srdivacky deleteOwnProperty(property); 679199989Srdivacky // add new data property 680202375Srdivacky addOwnProperty(key, propFlags, newDesc.getValue()); 681199989Srdivacky } else if (type == PropertyDescriptor.ACCESSOR) { 682193323Sed if (property == null) { 683193323Sed addOwnProperty(key, propFlags, 684198396Srdivacky newDesc.has(GET) ? newDesc.getGetter() : null, 685202375Srdivacky newDesc.has(SET) ? newDesc.getSetter() : null); 686199989Srdivacky } else { 687202375Srdivacky // Modify old property with the new features. 688199989Srdivacky modifyOwnProperty(property, propFlags, 689193323Sed newDesc.has(GET) ? newDesc.getGetter() : null, 690193323Sed newDesc.has(SET) ? newDesc.getSetter() : null); 691193323Sed } 692193323Sed } 693202878Srdivacky } 694202878Srdivacky 695202878Srdivacky checkIntegerKey(key); 696202878Srdivacky 697202878Srdivacky return true; 698202878Srdivacky } 699202878Srdivacky 700202878Srdivacky /** 701198396Srdivacky * Almost like defineOwnProperty(int,Object) for arrays this one does 702193323Sed * not add 'gap' elements (like the array one does). 703193323Sed * 704193323Sed * @param index key for property 705193323Sed * @param value value to define 706202878Srdivacky */ 707202878Srdivacky public void defineOwnProperty(final int index, final Object value) { 708202878Srdivacky assert isValidArrayIndex(index) : "invalid array index"; 709202878Srdivacky final long longIndex = ArrayIndex.toLongIndex(index); 710202878Srdivacky final long oldLength = getArray().length(); 711202878Srdivacky if (longIndex >= oldLength) { 712202878Srdivacky setArray(getArray().ensure(longIndex).safeDelete(oldLength, longIndex - 1, false)); 713202878Srdivacky } 714198396Srdivacky setArray(getArray().set(index, value, false)); 715193323Sed } 716193323Sed 717193323Sed private void checkIntegerKey(final Object key) { 718193323Sed final int index = getArrayIndex(key); 719202878Srdivacky 720202878Srdivacky if (isValidArrayIndex(index)) { 721202878Srdivacky final ArrayData data = getArray(); 722202878Srdivacky 723202878Srdivacky if (data.has(index)) { 724202878Srdivacky setArray(data.delete(index)); 725202878Srdivacky } 726202878Srdivacky } 727198396Srdivacky } 728193323Sed 729193323Sed /** 730193323Sed * Add a new property to the object. 731193323Sed * 732202878Srdivacky * @param key property key 733202878Srdivacky * @param propertyDesc property descriptor for property 734202878Srdivacky */ 735202878Srdivacky public final void addOwnProperty(final Object key, final PropertyDescriptor propertyDesc) { 736202878Srdivacky // Already checked that there is no own property with that key. 737202878Srdivacky PropertyDescriptor pdesc = propertyDesc; 738202878Srdivacky 739202878Srdivacky final int propFlags = Property.toFlags(pdesc); 740198396Srdivacky 741193323Sed if (pdesc.type() == PropertyDescriptor.GENERIC) { 742193323Sed final Global global = Context.getGlobal(); 743193323Sed final PropertyDescriptor dDesc = global.newDataDescriptor(UNDEFINED, false, false, false); 744198396Srdivacky 745218893Sdim dDesc.fillFrom((ScriptObject)pdesc); 746193323Sed pdesc = dDesc; 747193323Sed } 748193323Sed 749207618Srdivacky final int type = pdesc.type(); 750193323Sed if (type == PropertyDescriptor.DATA) { 751193323Sed addOwnProperty(key, propFlags, pdesc.getValue()); 752193323Sed } else if (type == PropertyDescriptor.ACCESSOR) { 753193323Sed addOwnProperty(key, propFlags, 754193323Sed pdesc.has(GET) ? pdesc.getGetter() : null, 755193323Sed pdesc.has(SET) ? pdesc.getSetter() : null); 756193323Sed } 757198396Srdivacky 758193323Sed checkIntegerKey(key); 759193323Sed } 760193323Sed 761198396Srdivacky /** 762193323Sed * Low level property API (not using property descriptors) 763193323Sed * <p> 764207618Srdivacky * Find a property in the prototype hierarchy. Note: this is final and not 765200581Srdivacky * a good idea to override. If you have to, use 766200581Srdivacky * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or 767200581Srdivacky * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the 768200581Srdivacky * overriding way to find array properties 769200581Srdivacky * 770200581Srdivacky * @see jdk.nashorn.internal.objects.NativeArray 771200581Srdivacky * 772202878Srdivacky * @param key Property key. 773202878Srdivacky * @param deep Whether the search should look up proto chain. 774200581Srdivacky * 775200581Srdivacky * @return FindPropertyData or null if not found. 776200581Srdivacky */ 777200581Srdivacky public final FindProperty findProperty(final Object key, final boolean deep) { 778200581Srdivacky return findProperty(key, deep, false, this); 779200581Srdivacky } 780200581Srdivacky 781200581Srdivacky /** 782200581Srdivacky * Low level property API (not using property descriptors) 783200581Srdivacky * <p> 784200581Srdivacky * Find a property in the prototype hierarchy. Note: this is not a good idea 785200581Srdivacky * to override except as it was done in {@link WithObject}. 786200581Srdivacky * If you have to, use 787200581Srdivacky * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or 788200581Srdivacky * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the 789200581Srdivacky * overriding way to find array properties 790200581Srdivacky * 791200581Srdivacky * @see jdk.nashorn.internal.objects.NativeArray 792200581Srdivacky * 793200581Srdivacky * @param key Property key. 794200581Srdivacky * @param deep true if the search should look up proto chain 795200581Srdivacky * @param isScope true if this is a scope access 796200581Srdivacky * @param start the object on which the lookup was originally initiated 797200581Srdivacky * @return FindPropertyData or null if not found. 798200581Srdivacky */ 799200581Srdivacky protected FindProperty findProperty(final Object key, final boolean deep, final boolean isScope, final ScriptObject start) { 800200581Srdivacky 801202878Srdivacky final PropertyMap selfMap = getMap(); 802200581Srdivacky final Property property = selfMap.findProperty(key); 803200581Srdivacky 804200581Srdivacky if (property != null) { 805202878Srdivacky return new FindProperty(start, this, property); 806200581Srdivacky } 807200581Srdivacky 808200581Srdivacky if (deep) { 809204642Srdivacky final ScriptObject myProto = getProto(); 810200581Srdivacky final FindProperty find = myProto == null ? null : myProto.findProperty(key, true, isScope, start); 811204642Srdivacky // checkSharedProtoMap must be invoked after myProto.checkSharedProtoMap to propagate 812200581Srdivacky // shared proto invalidation up the prototype chain. It also must be invoked when prototype is null. 813200581Srdivacky checkSharedProtoMap(); 814202878Srdivacky return find; 815204642Srdivacky } 816204642Srdivacky 817204642Srdivacky return null; 818200581Srdivacky } 819200581Srdivacky 820200581Srdivacky /** 821200581Srdivacky * Low level property API. This is similar to {@link #findProperty(Object, boolean)} but returns a 822200581Srdivacky * {@code boolean} value instead of a {@link FindProperty} object. 823200581Srdivacky * @param key Property key. 824202878Srdivacky * @param deep Whether the search should look up proto chain. 825200581Srdivacky * @return true if the property was found. 826200581Srdivacky */ 827200581Srdivacky boolean hasProperty(final Object key, final boolean deep) { 828200581Srdivacky if (getMap().findProperty(key) != null) { 829200581Srdivacky return true; 830200581Srdivacky } 831200581Srdivacky 832200581Srdivacky if (deep) { 833200581Srdivacky final ScriptObject myProto = getProto(); 834218893Sdim if (myProto != null) { 835200581Srdivacky return myProto.hasProperty(key, true); 836200581Srdivacky } 837200581Srdivacky } 838200581Srdivacky 839200581Srdivacky return false; 840200581Srdivacky } 841200581Srdivacky 842200581Srdivacky private SwitchPoint findBuiltinSwitchPoint(final Object key) { 843200581Srdivacky for (ScriptObject myProto = getProto(); myProto != null; myProto = myProto.getProto()) { 844207618Srdivacky final Property prop = myProto.getMap().findProperty(key); 845207618Srdivacky if (prop != null) { 846193323Sed final SwitchPoint sp = prop.getBuiltinSwitchPoint(); 847193323Sed if (sp != null && !sp.hasBeenInvalidated()) { 848193323Sed return sp; 849193323Sed } 850193323Sed } 851193323Sed } 852193323Sed return null; 853198396Srdivacky } 854193323Sed 855193323Sed /** 856218893Sdim * Add a new property to the object. 857193323Sed * <p> 858193323Sed * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s 859193323Sed * 860198396Srdivacky * @param key Property key. 861193323Sed * @param propertyFlags Property flags. 862193323Sed * @param getter Property getter, or null if not defined 863193323Sed * @param setter Property setter, or null if not defined 864193323Sed * 865193323Sed * @return New property. 866193323Sed */ 867207618Srdivacky public final Property addOwnProperty(final Object key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 868193323Sed return addOwnProperty(newUserAccessors(key, propertyFlags, getter, setter)); 869198090Srdivacky } 870193323Sed 871193323Sed /** 872193323Sed * Add a new property to the object. 873193323Sed * <p> 874193323Sed * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s 875193323Sed * 876193323Sed * @param key Property key. 877193323Sed * @param propertyFlags Property flags. 878193323Sed * @param value Value of property 879207618Srdivacky * 880207618Srdivacky * @return New property. 881200581Srdivacky */ 882200581Srdivacky public final Property addOwnProperty(final Object key, final int propertyFlags, final Object value) { 883200581Srdivacky return addSpillProperty(key, propertyFlags, value, true); 884200581Srdivacky } 885200581Srdivacky 886200581Srdivacky /** 887200581Srdivacky * Add a new property to the object. 888200581Srdivacky * <p> 889210299Sed * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s 890200581Srdivacky * 891200581Srdivacky * @param newProperty property to add 892200581Srdivacky * 893200581Srdivacky * @return New property. 894200581Srdivacky */ 895200581Srdivacky public final Property addOwnProperty(final Property newProperty) { 896207618Srdivacky PropertyMap oldMap = getMap(); 897207618Srdivacky while (true) { 898208599Srdivacky final PropertyMap newMap = oldMap.addProperty(newProperty); 899208599Srdivacky if (!compareAndSetMap(oldMap, newMap)) { 900208599Srdivacky oldMap = getMap(); 901200581Srdivacky final Property oldProperty = oldMap.findProperty(newProperty.getKey()); 902200581Srdivacky 903200581Srdivacky if (oldProperty != null) { 904200581Srdivacky return oldProperty; 905200581Srdivacky } 906200581Srdivacky } else { 907200581Srdivacky return newProperty; 908200581Srdivacky } 909200581Srdivacky } 910200581Srdivacky } 911218893Sdim 912200581Srdivacky private void erasePropertyValue(final Property property) { 913200581Srdivacky // Erase the property field value with undefined. If the property is an accessor property 914200581Srdivacky // we don't want to call the setter!! 915200581Srdivacky if (property != null && !property.isAccessorProperty()) { 916200581Srdivacky property.setValue(this, this, UNDEFINED, false); 917218893Sdim } 918200581Srdivacky } 919200581Srdivacky 920207618Srdivacky /** 921207618Srdivacky * Delete a property from the object. 922200581Srdivacky * 923200581Srdivacky * @param property Property to delete. 924208599Srdivacky * 925200581Srdivacky * @return true if deleted. 926200581Srdivacky */ 927200581Srdivacky public final boolean deleteOwnProperty(final Property property) { 928200581Srdivacky erasePropertyValue(property); 929200581Srdivacky PropertyMap oldMap = getMap(); 930200581Srdivacky 931218893Sdim while (true) { 932218893Sdim final PropertyMap newMap = oldMap.deleteProperty(property); 933203954Srdivacky 934200581Srdivacky if (newMap == null) { 935200581Srdivacky return false; 936200581Srdivacky } 937199481Srdivacky 938199481Srdivacky if (!compareAndSetMap(oldMap, newMap)) { 939199481Srdivacky oldMap = getMap(); 940199481Srdivacky } else { 941199481Srdivacky // delete getter and setter function references so that we don't leak 942199481Srdivacky if (property instanceof UserAccessorProperty) { 943199481Srdivacky ((UserAccessorProperty)property).setAccessors(this, getMap(), null); 944199481Srdivacky } 945199481Srdivacky 946199481Srdivacky invalidateGlobalConstant(property.getKey()); 947199481Srdivacky return true; 948199481Srdivacky } 949199481Srdivacky } 950199481Srdivacky 951199481Srdivacky } 952199481Srdivacky 953199481Srdivacky /** 954199481Srdivacky * Fast initialization functions for ScriptFunctions that are strict, to avoid 955199481Srdivacky * creating setters that probably aren't used. Inject directly into the spill pool 956199481Srdivacky * the defaults for "arguments" and "caller" 957199481Srdivacky * 958199481Srdivacky * @param key property key 959199481Srdivacky * @param propertyFlags flags 960199481Srdivacky * @param getter getter for {@link UserAccessorProperty}, null if not present or N/A 961199481Srdivacky * @param setter setter for {@link UserAccessorProperty}, null if not present or N/A 962199481Srdivacky */ 963199481Srdivacky protected final void initUserAccessors(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 964199481Srdivacky final PropertyMap oldMap = getMap(); 965199481Srdivacky final int slot = oldMap.getFreeSpillSlot(); 966199481Srdivacky ensureSpillSize(slot); 967199481Srdivacky objectSpill[slot] = new UserAccessorProperty.Accessors(getter, setter); 968199481Srdivacky Property newProperty; 969199481Srdivacky PropertyMap newMap; 970199481Srdivacky do { 971199481Srdivacky newProperty = new UserAccessorProperty(key, propertyFlags, slot); 972199481Srdivacky newMap = oldMap.addProperty(newProperty); 973193323Sed } while (!compareAndSetMap(oldMap, newMap)); 974193323Sed } 975193323Sed 976193323Sed /** 977200581Srdivacky * Modify a property in the object 978193323Sed * 979193323Sed * @param oldProperty property to modify 980193323Sed * @param propertyFlags new property flags 981193323Sed * @param getter getter for {@link UserAccessorProperty}, null if not present or N/A 982193323Sed * @param setter setter for {@link UserAccessorProperty}, null if not present or N/A 983193323Sed * 984193323Sed * @return new property 985193323Sed */ 986200581Srdivacky public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 987200581Srdivacky Property newProperty; 988193323Sed 989193323Sed if (oldProperty instanceof UserAccessorProperty) { 990193323Sed final UserAccessorProperty uc = (UserAccessorProperty)oldProperty; 991226633Sdim final int slot = uc.getSlot(); 992226633Sdim 993203954Srdivacky assert uc.getLocalType() == Object.class; 994202878Srdivacky final UserAccessorProperty.Accessors gs = uc.getAccessors(this); //this crashes 995202878Srdivacky assert gs != null; 996202878Srdivacky //reuse existing getter setter for speed 997202878Srdivacky gs.set(getter, setter); 998202878Srdivacky if (uc.getFlags() == (propertyFlags | Property.IS_ACCESSOR_PROPERTY)) { 999202878Srdivacky return oldProperty; 1000202878Srdivacky } 1001202878Srdivacky newProperty = new UserAccessorProperty(uc.getKey(), propertyFlags, slot); 1002202878Srdivacky } else { 1003202878Srdivacky // erase old property value and create new user accessor property 1004202878Srdivacky erasePropertyValue(oldProperty); 1005202878Srdivacky newProperty = newUserAccessors(oldProperty.getKey(), propertyFlags, getter, setter); 1006226633Sdim } 1007202878Srdivacky 1008203954Srdivacky return modifyOwnProperty(oldProperty, newProperty); 1009202878Srdivacky } 1010202878Srdivacky 1011202878Srdivacky /** 1012202878Srdivacky * Modify a property in the object 1013202878Srdivacky * 1014202878Srdivacky * @param oldProperty property to modify 1015202878Srdivacky * @param propertyFlags new property flags 1016193323Sed * 1017193323Sed * @return new property 1018193323Sed */ 1019193323Sed public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags) { 1020193323Sed return modifyOwnProperty(oldProperty, oldProperty.setFlags(propertyFlags)); 1021200581Srdivacky } 1022207618Srdivacky 1023200581Srdivacky /** 1024200581Srdivacky * Modify a property in the object, replacing a property with a new one 1025200581Srdivacky * 1026200581Srdivacky * @param oldProperty property to replace 1027200581Srdivacky * @param newProperty property to replace it with 1028200581Srdivacky * 1029200581Srdivacky * @return new property 1030200581Srdivacky */ 1031200581Srdivacky private Property modifyOwnProperty(final Property oldProperty, final Property newProperty) { 1032200581Srdivacky if (oldProperty == newProperty) { 1033200581Srdivacky return newProperty; //nop 1034200581Srdivacky } 1035200581Srdivacky 1036200581Srdivacky assert newProperty.getKey().equals(oldProperty.getKey()) : "replacing property with different key"; 1037200581Srdivacky 1038200581Srdivacky PropertyMap oldMap = getMap(); 1039200581Srdivacky 1040200581Srdivacky while (true) { 1041200581Srdivacky final PropertyMap newMap = oldMap.replaceProperty(oldProperty, newProperty); 1042200581Srdivacky 1043200581Srdivacky if (!compareAndSetMap(oldMap, newMap)) { 1044200581Srdivacky oldMap = getMap(); 1045200581Srdivacky final Property oldPropertyLookup = oldMap.findProperty(oldProperty.getKey()); 1046200581Srdivacky 1047200581Srdivacky if (oldPropertyLookup != null && oldPropertyLookup.equals(newProperty)) { 1048200581Srdivacky return oldPropertyLookup; 1049200581Srdivacky } 1050200581Srdivacky } else { 1051200581Srdivacky return newProperty; 1052200581Srdivacky } 1053200581Srdivacky } 1054200581Srdivacky } 1055200581Srdivacky 1056200581Srdivacky /** 1057200581Srdivacky * Update getter and setter in an object literal. 1058200581Srdivacky * 1059200581Srdivacky * @param key Property key. 1060200581Srdivacky * @param getter {@link UserAccessorProperty} defined getter, or null if none 1061200581Srdivacky * @param setter {@link UserAccessorProperty} defined setter, or null if none 1062200581Srdivacky */ 1063200581Srdivacky public final void setUserAccessors(final String key, final ScriptFunction getter, final ScriptFunction setter) { 1064200581Srdivacky final Property oldProperty = getMap().findProperty(key); 1065200581Srdivacky if (oldProperty instanceof UserAccessorProperty) { 1066200581Srdivacky modifyOwnProperty(oldProperty, oldProperty.getFlags(), getter, setter); 1067200581Srdivacky } else { 1068200581Srdivacky addOwnProperty(newUserAccessors(key, oldProperty != null ? oldProperty.getFlags() : 0, getter, setter)); 1069200581Srdivacky } 1070200581Srdivacky } 1071200581Srdivacky 1072210299Sed private static int getIntValue(final FindProperty find, final int programPoint) { 1073210299Sed final MethodHandle getter = find.getGetter(int.class, programPoint, null); 1074210299Sed if (getter != null) { 1075210299Sed try { 1076200581Srdivacky return (int)getter.invokeExact((Object)find.getGetterReceiver()); 1077200581Srdivacky } catch (final Error|RuntimeException e) { 1078200581Srdivacky throw e; 1079200581Srdivacky } catch (final Throwable e) { 1080200581Srdivacky throw new RuntimeException(e); 1081200581Srdivacky } 1082200581Srdivacky } 1083200581Srdivacky 1084200581Srdivacky return UNDEFINED_INT; 1085200581Srdivacky } 1086200581Srdivacky 1087200581Srdivacky private static double getDoubleValue(final FindProperty find, final int programPoint) { 1088200581Srdivacky final MethodHandle getter = find.getGetter(double.class, programPoint, null); 1089200581Srdivacky if (getter != null) { 1090200581Srdivacky try { 1091200581Srdivacky return (double)getter.invokeExact((Object)find.getGetterReceiver()); 1092200581Srdivacky } catch (final Error|RuntimeException e) { 1093200581Srdivacky throw e; 1094202375Srdivacky } catch (final Throwable e) { 1095202375Srdivacky throw new RuntimeException(e); 1096200581Srdivacky } 1097200581Srdivacky } 1098200581Srdivacky 1099200581Srdivacky return UNDEFINED_DOUBLE; 1100200581Srdivacky } 1101200581Srdivacky 1102200581Srdivacky /** 1103200581Srdivacky * Return methodHandle of value function for call. 1104200581Srdivacky * 1105200581Srdivacky * @param find data from find property. 1106200581Srdivacky * @param type method type of function. 1107200581Srdivacky * @param bindName null or name to bind to second argument (property not found method.) 1108200581Srdivacky * 1109200581Srdivacky * @return value of property as a MethodHandle or null. 1110200581Srdivacky */ 1111200581Srdivacky protected MethodHandle getCallMethodHandle(final FindProperty find, final MethodType type, final String bindName) { 1112200581Srdivacky return getCallMethodHandle(find.getObjectValue(), type, bindName); 1113200581Srdivacky } 1114200581Srdivacky 1115200581Srdivacky /** 1116200581Srdivacky * Return methodHandle of value function for call. 1117200581Srdivacky * 1118200581Srdivacky * @param value value of receiver, it not a {@link ScriptFunction} this will return null. 1119200581Srdivacky * @param type method type of function. 1120200581Srdivacky * @param bindName null or name to bind to second argument (property not found method.) 1121210299Sed * 1122200581Srdivacky * @return value of property as a MethodHandle or null. 1123200581Srdivacky */ 1124200581Srdivacky protected static MethodHandle getCallMethodHandle(final Object value, final MethodType type, final String bindName) { 1125210299Sed return value instanceof ScriptFunction ? ((ScriptFunction)value).getCallMethodHandle(type, bindName) : null; 1126200581Srdivacky } 1127200581Srdivacky 1128200581Srdivacky /** 1129200581Srdivacky * Get value using found property. 1130193323Sed * 1131207618Srdivacky * @param property Found property. 1132200581Srdivacky * 1133200581Srdivacky * @return Value of property. 1134200581Srdivacky */ 1135200581Srdivacky public final Object getWithProperty(final Property property) { 1136200581Srdivacky return new FindProperty(this, this, property).getObjectValue(); 1137207618Srdivacky } 1138200581Srdivacky 1139193323Sed /** 1140193323Sed * Get a property given a key 1141200581Srdivacky * 1142200581Srdivacky * @param key property key 1143193323Sed * 1144193323Sed * @return property for key 1145193323Sed */ 1146193323Sed public final Property getProperty(final String key) { 1147193323Sed return getMap().findProperty(key); 1148193323Sed } 1149193323Sed 1150193323Sed /** 1151193323Sed * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) 1152193323Sed * Used for argument access in a vararg function using parameter name. 1153193323Sed * Returns the argument at a given key (index) 1154193323Sed * 1155193323Sed * @param key argument index 1156193323Sed * 1157193323Sed * @return the argument at the given position, or undefined if not present 1158193323Sed */ 1159193323Sed public Object getArgument(final int key) { 1160193323Sed return get(key); 1161193323Sed } 1162193323Sed 1163193323Sed /** 1164193323Sed * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) 1165193323Sed * Used for argument access in a vararg function using parameter name. 1166193323Sed * Returns the argument at a given key (index) 1167210299Sed * 1168210299Sed * @param key argument index 1169210299Sed * @param value the value to write at the given index 1170210299Sed */ 1171193323Sed public void setArgument(final int key, final Object value) { 1172193323Sed set(key, value, 0); 1173193323Sed } 1174193323Sed 1175210299Sed /** 1176210299Sed * Return the current context from the object's map. 1177210299Sed * @return Current context. 1178210299Sed */ 1179193323Sed protected Context getContext() { 1180193323Sed return Context.fromClass(getClass()); 1181193323Sed } 1182193323Sed 1183193323Sed /** 1184193323Sed * Return the map of an object. 1185193323Sed * @return PropertyMap object. 1186193323Sed */ 1187193323Sed public final PropertyMap getMap() { 1188193323Sed return map; 1189193323Sed } 1190193323Sed 1191210299Sed /** 1192193323Sed * Set the initial map. 1193193323Sed * @param map Initial map. 1194193323Sed */ 1195193323Sed public final void setMap(final PropertyMap map) { 1196210299Sed this.map = map; 1197193323Sed } 1198193323Sed 1199 /** 1200 * Conditionally set the new map if the old map is the same. 1201 * @param oldMap Map prior to manipulation. 1202 * @param newMap Replacement map. 1203 * @return true if the operation succeeded. 1204 */ 1205 protected final boolean compareAndSetMap(final PropertyMap oldMap, final PropertyMap newMap) { 1206 if (oldMap == this.map) { 1207 this.map = newMap; 1208 return true; 1209 } 1210 return false; 1211 } 1212 1213 /** 1214 * Return the __proto__ of an object. 1215 * @return __proto__ object. 1216 */ 1217 public final ScriptObject getProto() { 1218 return proto; 1219 } 1220 1221 /** 1222 * Get the proto of a specific depth 1223 * @param n depth 1224 * @return proto at given depth 1225 */ 1226 public final ScriptObject getProto(final int n) { 1227 assert n > 0; 1228 ScriptObject p = getProto(); 1229 for (int i = n; --i > 0;) { 1230 p = p.getProto(); 1231 } 1232 return p; 1233 } 1234 1235 /** 1236 * Set the __proto__ of an object. 1237 * @param newProto new __proto__ to set. 1238 */ 1239 public final void setProto(final ScriptObject newProto) { 1240 final ScriptObject oldProto = proto; 1241 1242 if (oldProto != newProto) { 1243 proto = newProto; 1244 1245 // Let current listeners know that the prototype has changed 1246 getMap().protoChanged(true); 1247 // Replace our current allocator map with one that is associated with the new prototype. 1248 setMap(getMap().changeProto(newProto)); 1249 } 1250 } 1251 1252 /** 1253 * Set the initial __proto__ of this object. This should be used instead of 1254 * {@link #setProto} if it is known that the current property map will not be 1255 * used on a new object with any other parent property map, so we can pass over 1256 * property map invalidation/evolution. 1257 * 1258 * @param initialProto the initial __proto__ to set. 1259 */ 1260 public void setInitialProto(final ScriptObject initialProto) { 1261 this.proto = initialProto; 1262 } 1263 1264 /** 1265 * Invoked from generated bytecode to initialize the prototype of object literals to the global Object prototype. 1266 * @param obj the object literal that needs to have its prototype initialized to the global Object prototype. 1267 */ 1268 public static void setGlobalObjectProto(final ScriptObject obj) { 1269 obj.setInitialProto(Global.objectPrototype()); 1270 } 1271 1272 /** 1273 * Set the __proto__ of an object with checks. 1274 * This is the built-in operation [[SetPrototypeOf]] 1275 * See ES6 draft spec: 9.1.2 [[SetPrototypeOf]] (V) 1276 * 1277 * @param newProto Prototype to set. 1278 */ 1279 public final void setPrototypeOf(final Object newProto) { 1280 if (newProto == null || newProto instanceof ScriptObject) { 1281 if (! isExtensible()) { 1282 // okay to set same proto again - even if non-extensible 1283 1284 if (newProto == getProto()) { 1285 return; 1286 } 1287 throw typeError("__proto__.set.non.extensible", ScriptRuntime.safeToString(this)); 1288 } 1289 1290 // check for circularity 1291 ScriptObject p = (ScriptObject)newProto; 1292 while (p != null) { 1293 if (p == this) { 1294 throw typeError("circular.__proto__.set", ScriptRuntime.safeToString(this)); 1295 } 1296 p = p.getProto(); 1297 } 1298 setProto((ScriptObject) newProto); 1299 } else { 1300 throw typeError("cant.set.proto.to.non.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(newProto)); 1301 } 1302 } 1303 1304 /** 1305 * Set the __proto__ of an object from an object literal. 1306 * See ES6 draft spec: B.3.1 __proto__ Property Names in 1307 * Object Initializers. Step 6 handling of "__proto__". 1308 * 1309 * @param newProto Prototype to set. 1310 */ 1311 public final void setProtoFromLiteral(final Object newProto) { 1312 if (newProto == null || newProto instanceof ScriptObject) { 1313 setPrototypeOf(newProto); 1314 } else { 1315 // Some non-object, non-null. Then, we need to set 1316 // Object.prototype as the new __proto__ 1317 // 1318 // var obj = { __proto__ : 34 }; 1319 // print(obj.__proto__ === Object.prototype); // => true 1320 setPrototypeOf(Global.objectPrototype()); 1321 } 1322 } 1323 1324 /** 1325 * return an array of all property keys - all inherited, non-enumerable included. 1326 * This is meant for source code completion by interactive shells or editors. 1327 * 1328 * @return Array of keys, order of properties is undefined. 1329 */ 1330 public String[] getAllKeys() { 1331 final Set<String> keys = new HashSet<>(); 1332 final Set<String> nonEnumerable = new HashSet<>(); 1333 for (ScriptObject self = this; self != null; self = self.getProto()) { 1334 keys.addAll(Arrays.asList(self.getOwnKeys(String.class, true, nonEnumerable))); 1335 } 1336 return keys.toArray(new String[0]); 1337 } 1338 1339 /** 1340 * Return an array of own property keys associated with the object. 1341 * 1342 * @param all True if to include non-enumerable keys. 1343 * @return Array of keys. 1344 */ 1345 public final String[] getOwnKeys(final boolean all) { 1346 return getOwnKeys(String.class, all, null); 1347 } 1348 1349 /** 1350 * Return an array of own property keys associated with the object. 1351 * 1352 * @param all True if to include non-enumerable keys. 1353 * @return Array of keys. 1354 */ 1355 public final Symbol[] getOwnSymbols(final boolean all) { 1356 return getOwnKeys(Symbol.class, all, null); 1357 } 1358 1359 /** 1360 * return an array of own property keys associated with the object. 1361 * 1362 * @param <T> the type returned keys. 1363 * @param type the type of keys to return, either {@code String.class} or {@code Symbol.class}. 1364 * @param all True if to include non-enumerable keys. 1365 * @param nonEnumerable set of non-enumerable properties seen already. Used to 1366 * filter out shadowed, but enumerable properties from proto children. 1367 * @return Array of keys. 1368 */ 1369 @SuppressWarnings("unchecked") 1370 protected <T> T[] getOwnKeys(final Class<T> type, final boolean all, final Set<T> nonEnumerable) { 1371 final List<Object> keys = new ArrayList<>(); 1372 final PropertyMap selfMap = this.getMap(); 1373 1374 final ArrayData array = getArray(); 1375 1376 if (type == String.class) { 1377 for (final Iterator<Long> iter = array.indexIterator(); iter.hasNext(); ) { 1378 keys.add(JSType.toString(iter.next().longValue())); 1379 } 1380 } 1381 1382 for (final Property property : selfMap.getProperties()) { 1383 final boolean enumerable = property.isEnumerable(); 1384 final Object key = property.getKey(); 1385 if (!type.isInstance(key)) { 1386 continue; 1387 } 1388 if (all) { 1389 keys.add(key); 1390 } else if (enumerable) { 1391 // either we don't have non-enumerable filter set or filter set 1392 // does not contain the current property. 1393 if (nonEnumerable == null || !nonEnumerable.contains(key)) { 1394 keys.add(key); 1395 } 1396 } else { 1397 // store this non-enumerable property for later proto walk 1398 if (nonEnumerable != null) { 1399 nonEnumerable.add((T) key); 1400 } 1401 } 1402 } 1403 1404 return keys.toArray((T[]) Array.newInstance(type, keys.size())); 1405 } 1406 1407 /** 1408 * Check if this ScriptObject has array entries. This means that someone has 1409 * set values with numeric keys in the object. 1410 * 1411 * @return true if array entries exists. 1412 */ 1413 public boolean hasArrayEntries() { 1414 return getArray().length() > 0 || getMap().containsArrayKeys(); 1415 } 1416 1417 /** 1418 * Return the valid JavaScript type name descriptor 1419 * 1420 * @return "Object" 1421 */ 1422 public String getClassName() { 1423 return "Object"; 1424 } 1425 1426 /** 1427 * {@code length} is a well known property. This is its getter. 1428 * Note that this *may* be optimized by other classes 1429 * 1430 * @return length property value for this ScriptObject 1431 */ 1432 public Object getLength() { 1433 return get("length"); 1434 } 1435 1436 /** 1437 * Stateless toString for ScriptObjects. 1438 * 1439 * @return string description of this object, e.g. {@code [object Object]} 1440 */ 1441 public String safeToString() { 1442 return "[object " + getClassName() + "]"; 1443 } 1444 1445 /** 1446 * Return the default value of the object with a given preferred type hint. 1447 * The preferred type hints are String.class for type String, Number.class 1448 * for type Number. <p> 1449 * 1450 * A <code>hint</code> of null means "no hint". 1451 * 1452 * ECMA 8.12.8 [[DefaultValue]](hint) 1453 * 1454 * @param typeHint the preferred type hint 1455 * @return the default value 1456 */ 1457 public Object getDefaultValue(final Class<?> typeHint) { 1458 // We delegate to Global, as the implementation uses dynamic call sites to invoke object's "toString" and 1459 // "valueOf" methods, and in order to avoid those call sites from becoming megamorphic when multiple contexts 1460 // are being executed in a long-running program, we move the code and their associated dynamic call sites 1461 // (Global.TO_STRING and Global.VALUE_OF) into per-context code. 1462 return Context.getGlobal().getDefaultValue(this, typeHint); 1463 } 1464 1465 /** 1466 * Checking whether a script object is an instance of another. Used 1467 * in {@link ScriptFunction} for hasInstance implementation, walks 1468 * the proto chain 1469 * 1470 * @param instance instance to check 1471 * @return true if 'instance' is an instance of this object 1472 */ 1473 public boolean isInstance(final ScriptObject instance) { 1474 return false; 1475 } 1476 1477 /** 1478 * Flag this ScriptObject as non extensible 1479 * 1480 * @return the object after being made non extensible 1481 */ 1482 public ScriptObject preventExtensions() { 1483 PropertyMap oldMap = getMap(); 1484 while (!compareAndSetMap(oldMap, getMap().preventExtensions())) { 1485 oldMap = getMap(); 1486 } 1487 1488 //invalidate any fast array setters 1489 final ArrayData array = getArray(); 1490 assert array != null; 1491 setArray(ArrayData.preventExtension(array)); 1492 return this; 1493 } 1494 1495 /** 1496 * Check whether if an Object (not just a ScriptObject) represents JavaScript array 1497 * 1498 * @param obj object to check 1499 * 1500 * @return true if array 1501 */ 1502 public static boolean isArray(final Object obj) { 1503 return obj instanceof ScriptObject && ((ScriptObject)obj).isArray(); 1504 } 1505 1506 /** 1507 * Check if this ScriptObject is an array 1508 * @return true if array 1509 */ 1510 public final boolean isArray() { 1511 return (flags & IS_ARRAY) != 0; 1512 } 1513 1514 /** 1515 * Flag this ScriptObject as being an array 1516 */ 1517 public final void setIsArray() { 1518 flags |= IS_ARRAY; 1519 } 1520 1521 /** 1522 * Check if this ScriptObject is an {@code arguments} vector 1523 * @return true if arguments vector 1524 */ 1525 public final boolean isArguments() { 1526 return (flags & IS_ARGUMENTS) != 0; 1527 } 1528 1529 /** 1530 * Flag this ScriptObject as being an {@code arguments} vector 1531 */ 1532 public final void setIsArguments() { 1533 flags |= IS_ARGUMENTS; 1534 } 1535 1536 /** 1537 * Check if this object has non-writable length property 1538 * 1539 * @return {@code true} if 'length' property is non-writable 1540 */ 1541 public boolean isLengthNotWritable() { 1542 return (flags & IS_LENGTH_NOT_WRITABLE) != 0; 1543 } 1544 1545 /** 1546 * Flag this object as having non-writable length property. 1547 */ 1548 public void setIsLengthNotWritable() { 1549 flags |= IS_LENGTH_NOT_WRITABLE; 1550 } 1551 1552 /** 1553 * Get the {@link ArrayData}, for this ScriptObject, ensuring it is of a type 1554 * that can handle elementType 1555 * @param elementType elementType 1556 * @return array data 1557 */ 1558 public final ArrayData getArray(final Class<?> elementType) { 1559 if (elementType == null) { 1560 return arrayData; 1561 } 1562 final ArrayData newArrayData = arrayData.convert(elementType); 1563 if (newArrayData != arrayData) { 1564 arrayData = newArrayData; 1565 } 1566 return newArrayData; 1567 } 1568 1569 /** 1570 * Get the {@link ArrayData} for this ScriptObject if it is an array 1571 * @return array data 1572 */ 1573 public final ArrayData getArray() { 1574 return arrayData; 1575 } 1576 1577 /** 1578 * Set the {@link ArrayData} for this ScriptObject if it is to be an array 1579 * @param arrayData the array data 1580 */ 1581 public final void setArray(final ArrayData arrayData) { 1582 this.arrayData = arrayData; 1583 } 1584 1585 /** 1586 * Check if this ScriptObject is extensible 1587 * @return true if extensible 1588 */ 1589 public boolean isExtensible() { 1590 return getMap().isExtensible(); 1591 } 1592 1593 /** 1594 * ECMAScript 15.2.3.8 - seal implementation 1595 * @return the sealed ScriptObject 1596 */ 1597 public ScriptObject seal() { 1598 PropertyMap oldMap = getMap(); 1599 1600 while (true) { 1601 final PropertyMap newMap = getMap().seal(); 1602 1603 if (!compareAndSetMap(oldMap, newMap)) { 1604 oldMap = getMap(); 1605 } else { 1606 setArray(ArrayData.seal(getArray())); 1607 return this; 1608 } 1609 } 1610 } 1611 1612 /** 1613 * Check whether this ScriptObject is sealed 1614 * @return true if sealed 1615 */ 1616 public boolean isSealed() { 1617 return getMap().isSealed(); 1618 } 1619 1620 /** 1621 * ECMA 15.2.39 - freeze implementation. Freeze this ScriptObject 1622 * @return the frozen ScriptObject 1623 */ 1624 public ScriptObject freeze() { 1625 PropertyMap oldMap = getMap(); 1626 1627 while (true) { 1628 final PropertyMap newMap = getMap().freeze(); 1629 1630 if (!compareAndSetMap(oldMap, newMap)) { 1631 oldMap = getMap(); 1632 } else { 1633 setArray(ArrayData.freeze(getArray())); 1634 return this; 1635 } 1636 } 1637 } 1638 1639 /** 1640 * Check whether this ScriptObject is frozen 1641 * @return true if frozen 1642 */ 1643 public boolean isFrozen() { 1644 return getMap().isFrozen(); 1645 } 1646 1647 /** 1648 * Check whether this ScriptObject is scope 1649 * @return true if scope 1650 */ 1651 public boolean isScope() { 1652 return false; 1653 } 1654 1655 /** 1656 * Tag this script object as built in 1657 */ 1658 public final void setIsBuiltin() { 1659 flags |= IS_BUILTIN; 1660 } 1661 1662 /** 1663 * Check if this script object is built in 1664 * @return true if build in 1665 */ 1666 public final boolean isBuiltin() { 1667 return (flags & IS_BUILTIN) != 0; 1668 } 1669 1670 /** 1671 * Clears the properties from a ScriptObject 1672 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1673 * 1674 * @param strict strict mode or not 1675 */ 1676 public void clear(final boolean strict) { 1677 final Iterator<String> iter = propertyIterator(); 1678 while (iter.hasNext()) { 1679 delete(iter.next(), strict); 1680 } 1681 } 1682 1683 /** 1684 * Checks if a property with a given key is present in a ScriptObject 1685 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1686 * 1687 * @param key the key to check for 1688 * @return true if a property with the given key exists, false otherwise 1689 */ 1690 public boolean containsKey(final Object key) { 1691 return has(key); 1692 } 1693 1694 /** 1695 * Checks if a property with a given value is present in a ScriptObject 1696 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1697 * 1698 * @param value value to check for 1699 * @return true if a property with the given value exists, false otherwise 1700 */ 1701 public boolean containsValue(final Object value) { 1702 final Iterator<Object> iter = valueIterator(); 1703 while (iter.hasNext()) { 1704 if (iter.next().equals(value)) { 1705 return true; 1706 } 1707 } 1708 return false; 1709 } 1710 1711 /** 1712 * Returns the set of {@literal <property, value>} entries that make up this 1713 * ScriptObject's properties 1714 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1715 * 1716 * @return an entry set of all the properties in this object 1717 */ 1718 public Set<Map.Entry<Object, Object>> entrySet() { 1719 final Iterator<String> iter = propertyIterator(); 1720 final Set<Map.Entry<Object, Object>> entries = new HashSet<>(); 1721 while (iter.hasNext()) { 1722 final Object key = iter.next(); 1723 entries.add(new AbstractMap.SimpleImmutableEntry<>(key, get(key))); 1724 } 1725 return Collections.unmodifiableSet(entries); 1726 } 1727 1728 /** 1729 * Check whether a ScriptObject contains no properties 1730 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1731 * 1732 * @return true if object has no properties 1733 */ 1734 public boolean isEmpty() { 1735 return !propertyIterator().hasNext(); 1736 } 1737 1738 /** 1739 * Return the set of keys (property names) for all properties 1740 * in this ScriptObject 1741 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1742 * 1743 * @return keySet of this ScriptObject 1744 */ 1745 public Set<Object> keySet() { 1746 final Iterator<String> iter = propertyIterator(); 1747 final Set<Object> keySet = new HashSet<>(); 1748 while (iter.hasNext()) { 1749 keySet.add(iter.next()); 1750 } 1751 return Collections.unmodifiableSet(keySet); 1752 } 1753 1754 /** 1755 * Put a property in the ScriptObject 1756 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1757 * 1758 * @param key property key 1759 * @param value property value 1760 * @param strict strict mode or not 1761 * @return oldValue if property with same key existed already 1762 */ 1763 public Object put(final Object key, final Object value, final boolean strict) { 1764 final Object oldValue = get(key); 1765 final int scriptObjectFlags = strict ? NashornCallSiteDescriptor.CALLSITE_STRICT : 0; 1766 set(key, value, scriptObjectFlags); 1767 return oldValue; 1768 } 1769 1770 /** 1771 * Put several properties in the ScriptObject given a mapping 1772 * of their keys to their values 1773 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1774 * 1775 * @param otherMap a {@literal <key,value>} map of properties to add 1776 * @param strict strict mode or not 1777 */ 1778 public void putAll(final Map<?, ?> otherMap, final boolean strict) { 1779 final int scriptObjectFlags = strict ? NashornCallSiteDescriptor.CALLSITE_STRICT : 0; 1780 for (final Map.Entry<?, ?> entry : otherMap.entrySet()) { 1781 set(entry.getKey(), entry.getValue(), scriptObjectFlags); 1782 } 1783 } 1784 1785 /** 1786 * Remove a property from the ScriptObject. 1787 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1788 * 1789 * @param key the key of the property 1790 * @param strict strict mode or not 1791 * @return the oldValue of the removed property 1792 */ 1793 public Object remove(final Object key, final boolean strict) { 1794 final Object oldValue = get(key); 1795 delete(key, strict); 1796 return oldValue; 1797 } 1798 1799 /** 1800 * Return the size of the ScriptObject - i.e. the number of properties 1801 * it contains 1802 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1803 * 1804 * @return number of properties in ScriptObject 1805 */ 1806 public int size() { 1807 int n = 0; 1808 for (final Iterator<String> iter = propertyIterator(); iter.hasNext(); iter.next()) { 1809 n++; 1810 } 1811 return n; 1812 } 1813 1814 /** 1815 * Return the values of the properties in the ScriptObject 1816 * (java.util.Map-like method to help ScriptObjectMirror implementation) 1817 * 1818 * @return collection of values for the properties in this ScriptObject 1819 */ 1820 public Collection<Object> values() { 1821 final List<Object> values = new ArrayList<>(size()); 1822 final Iterator<Object> iter = valueIterator(); 1823 while (iter.hasNext()) { 1824 values.add(iter.next()); 1825 } 1826 return Collections.unmodifiableList(values); 1827 } 1828 1829 /** 1830 * Lookup method that, given a CallSiteDescriptor, looks up the target 1831 * MethodHandle and creates a GuardedInvocation 1832 * with the appropriate guard(s). 1833 * 1834 * @param desc call site descriptor 1835 * @param request the link request 1836 * 1837 * @return GuardedInvocation for the callsite 1838 */ 1839 public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) { 1840 // NOTE: we support GET_ELEMENT and SET_ELEMENT as JavaScript doesn't distinguish items from properties. Nashorn itself 1841 // emits "GET_PROPERTY|GET_ELEMENT|GET_METHOD:identifier" for "<expr>.<identifier>" and "GET_ELEMENT|GET_PROPERTY|GET_METHOD" for "<expr>[<expr>]", but we are 1842 // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the 1843 // operation has an associated name or not. 1844 final StandardOperation op = NashornCallSiteDescriptor.getFirstStandardOperation(desc); 1845 if (op == null) { 1846 return null; 1847 } 1848 switch (op) { 1849 case GET_PROPERTY: 1850 case GET_ELEMENT: 1851 case GET_METHOD: 1852 return desc.getOperation() instanceof NamedOperation 1853 ? findGetMethod(desc, request, op) 1854 : findGetIndexMethod(desc, request); 1855 case SET_PROPERTY: 1856 case SET_ELEMENT: 1857 return desc.getOperation() instanceof NamedOperation 1858 ? findSetMethod(desc, request) 1859 : findSetIndexMethod(desc, request); 1860 case CALL: 1861 return findCallMethod(desc, request); 1862 case NEW: 1863 return findNewMethod(desc, request); 1864 case CALL_METHOD: 1865 return findCallMethodMethod(desc, request); 1866 default: 1867 } 1868 return null; 1869 } 1870 1871 /** 1872 * Find the appropriate New method for an invoke dynamic call. 1873 * 1874 * @param desc The invoke dynamic call site descriptor. 1875 * @param request The link request 1876 * 1877 * @return GuardedInvocation to be invoked at call site. 1878 */ 1879 protected GuardedInvocation findNewMethod(final CallSiteDescriptor desc, final LinkRequest request) { 1880 return notAFunction(desc); 1881 } 1882 1883 /** 1884 * Find the appropriate CALL method for an invoke dynamic call. 1885 * This generates "not a function" always 1886 * 1887 * @param desc the call site descriptor. 1888 * @param request the link request 1889 * 1890 * @return GuardedInvocation to be invoked at call site. 1891 */ 1892 protected GuardedInvocation findCallMethod(final CallSiteDescriptor desc, final LinkRequest request) { 1893 return notAFunction(desc); 1894 } 1895 1896 private GuardedInvocation notAFunction(final CallSiteDescriptor desc) { 1897 throw typeError("not.a.function", NashornCallSiteDescriptor.getFunctionErrorMessage(desc, this)); 1898 } 1899 1900 /** 1901 * Find an implementation for a CALL_METHOD operation. Note that Nashorn internally never uses 1902 * CALL_METHOD, but instead always emits two call sites in bytecode, one for GET_METHOD, and then another 1903 * one for CALL. Explicit support for CALL_METHOD is provided for the benefit of potential external 1904 * callers. The implementation itself actually folds a GET_METHOD method handle into a CALL method handle. 1905 * 1906 * @param desc the call site descriptor. 1907 * @param request the link request 1908 * 1909 * @return GuardedInvocation to be invoked at call site. 1910 */ 1911 protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) { 1912 // R(P0, P1, ...) 1913 final MethodType callType = desc.getMethodType(); 1914 // use type Object(P0) for the getter 1915 final CallSiteDescriptor getterType = desc.changeMethodType(MethodType.methodType(Object.class, callType.parameterType(0))); 1916 final GuardedInvocation getter = findGetMethod(getterType, request, StandardOperation.GET_METHOD); 1917 1918 // Object(P0) => Object(P0, P1, ...) 1919 final MethodHandle argDroppingGetter = MH.dropArguments(getter.getInvocation(), 1, callType.parameterList().subList(1, callType.parameterCount())); 1920 // R(Object, P0, P1, ...) 1921 final MethodHandle invoker = Bootstrap.createDynamicInvoker("", NashornCallSiteDescriptor.CALL, callType.insertParameterTypes(0, argDroppingGetter.type().returnType())); 1922 // Fold Object(P0, P1, ...) into R(Object, P0, P1, ...) => R(P0, P1, ...) 1923 return getter.replaceMethods(MH.foldArguments(invoker, argDroppingGetter), getter.getGuard()); 1924 } 1925 1926 /** 1927 * Test whether this object contains in its prototype chain or is itself a with-object. 1928 * @return true if a with-object was found 1929 */ 1930 boolean hasWithScope() { 1931 return false; 1932 } 1933 1934 /** 1935 * Add a filter to the first argument of {@code methodHandle} that calls its {@link #getProto()} method 1936 * {@code depth} times. 1937 * @param methodHandle a method handle 1938 * @param depth distance to target prototype 1939 * @return the filtered method handle 1940 */ 1941 static MethodHandle addProtoFilter(final MethodHandle methodHandle, final int depth) { 1942 if (depth == 0) { 1943 return methodHandle; 1944 } 1945 final int listIndex = depth - 1; // We don't need 0-deep walker 1946 MethodHandle filter = listIndex < PROTO_FILTERS.size() ? PROTO_FILTERS.get(listIndex) : null; 1947 1948 if (filter == null) { 1949 filter = addProtoFilter(GETPROTO, depth - 1); 1950 PROTO_FILTERS.add(null); 1951 PROTO_FILTERS.set(listIndex, filter); 1952 } 1953 1954 return MH.filterArguments(methodHandle, 0, filter.asType(filter.type().changeReturnType(methodHandle.type().parameterType(0)))); 1955 } 1956 1957 /** 1958 * Find the appropriate GET method for an invoke dynamic call. 1959 * 1960 * @param desc the call site descriptor 1961 * @param request the link request 1962 * @param operation operation for get: getProp, getMethod, getElem etc 1963 * 1964 * @return GuardedInvocation to be invoked at call site. 1965 */ 1966 protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final StandardOperation operation) { 1967 final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request); 1968 1969 String name = NashornCallSiteDescriptor.getOperand(desc); 1970 if (NashornCallSiteDescriptor.isApplyToCall(desc)) { 1971 if (Global.isBuiltinFunctionPrototypeApply()) { 1972 name = "call"; 1973 } 1974 } 1975 1976 if (request.isCallSiteUnstable() || hasWithScope()) { 1977 return findMegaMorphicGetMethod(desc, name, operation == StandardOperation.GET_METHOD); 1978 } 1979 1980 final FindProperty find = findProperty(name, true, NashornCallSiteDescriptor.isScope(desc), this); 1981 MethodHandle mh; 1982 1983 if (find == null) { 1984 switch (operation) { 1985 case GET_ELEMENT: // getElem only gets here if element name is constant, so treat it like a property access 1986 case GET_PROPERTY: 1987 return noSuchProperty(desc, request); 1988 case GET_METHOD: 1989 return noSuchMethod(desc, request); 1990 default: 1991 throw new AssertionError(operation); // never invoked with any other operation 1992 } 1993 } 1994 1995 final GlobalConstants globalConstants = getGlobalConstants(); 1996 if (globalConstants != null) { 1997 final GuardedInvocation cinv = globalConstants.findGetMethod(find, this, desc); 1998 if (cinv != null) { 1999 return cinv; 2000 } 2001 } 2002 2003 final Class<?> returnType = desc.getMethodType().returnType(); 2004 final Property property = find.getProperty(); 2005 2006 final int programPoint = NashornCallSiteDescriptor.isOptimistic(desc) ? 2007 NashornCallSiteDescriptor.getProgramPoint(desc) : 2008 UnwarrantedOptimismException.INVALID_PROGRAM_POINT; 2009 2010 mh = find.getGetter(returnType, programPoint, request); 2011 // Get the appropriate guard for this callsite and property. 2012 final MethodHandle guard = NashornGuards.getGuard(this, property, desc, explicitInstanceOfCheck); 2013 final ScriptObject owner = find.getOwner(); 2014 final Class<ClassCastException> exception = explicitInstanceOfCheck ? null : ClassCastException.class; 2015 2016 final SwitchPoint[] protoSwitchPoints; 2017 2018 if (mh == null) { 2019 mh = Lookup.emptyGetter(returnType); 2020 protoSwitchPoints = getProtoSwitchPoints(name, owner); 2021 } else if (!find.isSelf()) { 2022 assert mh.type().returnType().equals(returnType) : 2023 "return type mismatch for getter " + mh.type().returnType() + " != " + returnType; 2024 if (!property.isAccessorProperty()) { 2025 // Add a filter that replaces the self object with the prototype owning the property. 2026 mh = addProtoFilter(mh, find.getProtoChainLength()); 2027 } 2028 protoSwitchPoints = getProtoSwitchPoints(name, owner); 2029 } else { 2030 protoSwitchPoints = null; 2031 } 2032 2033 final GuardedInvocation inv = new GuardedInvocation(mh, guard, protoSwitchPoints, exception); 2034 return inv.addSwitchPoint(findBuiltinSwitchPoint(name)); 2035 } 2036 2037 private static GuardedInvocation findMegaMorphicGetMethod(final CallSiteDescriptor desc, final String name, final boolean isMethod) { 2038 Context.getContextTrusted().getLogger(ObjectClassGenerator.class).warning("Megamorphic getter: ", desc, " ", name + " ", isMethod); 2039 final MethodHandle invoker = MH.insertArguments(MEGAMORPHIC_GET, 1, name, isMethod, NashornCallSiteDescriptor.isScope(desc)); 2040 final MethodHandle guard = getScriptObjectGuard(desc.getMethodType(), true); 2041 return new GuardedInvocation(invoker, guard); 2042 } 2043 2044 @SuppressWarnings("unused") 2045 private Object megamorphicGet(final String key, final boolean isMethod, final boolean isScope) { 2046 final FindProperty find = findProperty(key, true, isScope, this); 2047 if (find != null) { 2048 return find.getObjectValue(); 2049 } 2050 2051 return isMethod ? getNoSuchMethod(key, isScope, INVALID_PROGRAM_POINT) : invokeNoSuchProperty(key, isScope, INVALID_PROGRAM_POINT); 2052 } 2053 2054 // Marks a property as declared and sets its value. Used as slow path for block-scoped LET and CONST 2055 @SuppressWarnings("unused") 2056 private void declareAndSet(final String key, final Object value) { 2057 final PropertyMap oldMap = getMap(); 2058 final FindProperty find = findProperty(key, false); 2059 assert find != null; 2060 2061 final Property property = find.getProperty(); 2062 assert property != null; 2063 assert property.needsDeclaration(); 2064 2065 final PropertyMap newMap = oldMap.replaceProperty(property, property.removeFlags(Property.NEEDS_DECLARATION)); 2066 setMap(newMap); 2067 set(key, value, 0); 2068 } 2069 2070 /** 2071 * Find the appropriate GETINDEX method for an invoke dynamic call. 2072 * 2073 * @param desc the call site descriptor 2074 * @param request the link request 2075 * 2076 * @return GuardedInvocation to be invoked at call site. 2077 */ 2078 protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) { 2079 final MethodType callType = desc.getMethodType(); 2080 final Class<?> returnType = callType.returnType(); 2081 final Class<?> returnClass = returnType.isPrimitive() ? returnType : Object.class; 2082 final Class<?> keyClass = callType.parameterType(1); 2083 final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request); 2084 2085 final String name; 2086 if (returnClass.isPrimitive()) { 2087 //turn e.g. get with a double into getDouble 2088 final String returnTypeName = returnClass.getName(); 2089 name = "get" + Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length()); 2090 } else { 2091 name = "get"; 2092 } 2093 2094 final MethodHandle mh = findGetIndexMethodHandle(returnClass, name, keyClass, desc); 2095 return new GuardedInvocation(mh, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class); 2096 } 2097 2098 private static MethodHandle getScriptObjectGuard(final MethodType type, final boolean explicitInstanceOfCheck) { 2099 return ScriptObject.class.isAssignableFrom(type.parameterType(0)) ? null : NashornGuards.getScriptObjectGuard(explicitInstanceOfCheck); 2100 } 2101 2102 /** 2103 * Find a handle for a getIndex method 2104 * @param returnType return type for getter 2105 * @param name name 2106 * @param elementType index type for getter 2107 * @param desc call site descriptor 2108 * @return method handle for getter 2109 */ 2110 protected MethodHandle findGetIndexMethodHandle(final Class<?> returnType, final String name, final Class<?> elementType, final CallSiteDescriptor desc) { 2111 if (!returnType.isPrimitive()) { 2112 return findOwnMH_V(getClass(), name, returnType, elementType); 2113 } 2114 2115 return MH.insertArguments( 2116 findOwnMH_V(getClass(), name, returnType, elementType, int.class), 2117 2, 2118 NashornCallSiteDescriptor.isOptimistic(desc) ? 2119 NashornCallSiteDescriptor.getProgramPoint(desc) : 2120 INVALID_PROGRAM_POINT); 2121 } 2122 2123 /** 2124 * Get a switch point for a property with the given {@code name} that will be invalidated when 2125 * the property definition is changed in this object's prototype chain. Returns {@code null} if 2126 * the property is defined in this object itself. 2127 * 2128 * @param name the property name 2129 * @param owner the property owner, null if property is not defined 2130 * @return a SwitchPoint or null 2131 */ 2132 public final SwitchPoint[] getProtoSwitchPoints(final String name, final ScriptObject owner) { 2133 if (owner == this || getProto() == null) { 2134 return null; 2135 } 2136 2137 final List<SwitchPoint> switchPoints = new ArrayList<>(); 2138 for (ScriptObject obj = this; obj != owner && obj.getProto() != null; obj = obj.getProto()) { 2139 final ScriptObject parent = obj.getProto(); 2140 parent.getMap().addListener(name, obj.getMap()); 2141 final SwitchPoint sp = parent.getMap().getSharedProtoSwitchPoint(); 2142 if (sp != null && !sp.hasBeenInvalidated()) { 2143 switchPoints.add(sp); 2144 } 2145 } 2146 2147 switchPoints.add(getMap().getSwitchPoint(name)); 2148 return switchPoints.toArray(new SwitchPoint[0]); 2149 } 2150 2151 private void checkSharedProtoMap() { 2152 // Check if our map has an expected shared prototype property map. If it has, make sure that 2153 // the prototype map has not been invalidated, and that it does match the actual map of the prototype. 2154 if (getMap().isInvalidSharedMapFor(getProto())) { 2155 // Change our own map to one that does not assume a shared prototype map. 2156 setMap(getMap().makeUnsharedCopy()); 2157 } 2158 } 2159 2160 /** 2161 * Find the appropriate SET method for an invoke dynamic call. 2162 * 2163 * @param desc the call site descriptor 2164 * @param request the link request 2165 * 2166 * @return GuardedInvocation to be invoked at call site. 2167 */ 2168 protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) { 2169 final String name = NashornCallSiteDescriptor.getOperand(desc); 2170 2171 if (request.isCallSiteUnstable() || hasWithScope()) { 2172 return findMegaMorphicSetMethod(desc, name); 2173 } 2174 2175 final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request); 2176 2177 /* 2178 * If doing property set on a scope object, we should stop proto search on the first 2179 * non-scope object. Without this, for example, when assigning "toString" on global scope, 2180 * we'll end up assigning it on it's proto - which is Object.prototype.toString !! 2181 * 2182 * toString = function() { print("global toString"); } // don't affect Object.prototype.toString 2183 */ 2184 FindProperty find = findProperty(name, true, NashornCallSiteDescriptor.isScope(desc), this); 2185 2186 // If it's not a scope search, then we don't want any inherited properties except those with user defined accessors. 2187 if (find != null && find.isInheritedOrdinaryProperty()) { 2188 // We should still check if inherited data property is not writable 2189 if (isExtensible() && !find.getProperty().isWritable()) { 2190 return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.not.writable", true); 2191 } 2192 // Otherwise, forget the found property unless this is a scope callsite and the owner is a scope object as well. 2193 if (!NashornCallSiteDescriptor.isScope(desc) || !find.getOwner().isScope()) { 2194 find = null; 2195 } 2196 } 2197 2198 if (find != null) { 2199 if (!find.getProperty().isWritable() && !NashornCallSiteDescriptor.isDeclaration(desc)) { 2200 if (NashornCallSiteDescriptor.isScope(desc) && find.getProperty().isLexicalBinding()) { 2201 throw typeError("assign.constant", name); // Overwriting ES6 const should throw also in non-strict mode. 2202 } 2203 // Existing, non-writable data property 2204 return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.not.writable", true); 2205 } 2206 if (!find.getProperty().hasNativeSetter()) { 2207 // Existing accessor property without setter 2208 return createEmptySetMethod(desc, explicitInstanceOfCheck, "property.has.no.setter", true); 2209 } 2210 } else { 2211 if (!isExtensible()) { 2212 return createEmptySetMethod(desc, explicitInstanceOfCheck, "object.non.extensible", false); 2213 } 2214 } 2215 2216 final GuardedInvocation inv = new SetMethodCreator(this, find, desc, request).createGuardedInvocation(findBuiltinSwitchPoint(name)); 2217 2218 final GlobalConstants globalConstants = getGlobalConstants(); 2219 if (globalConstants != null) { 2220 final GuardedInvocation cinv = globalConstants.findSetMethod(find, this, inv, desc, request); 2221 if (cinv != null) { 2222 return cinv; 2223 } 2224 } 2225 2226 return inv; 2227 } 2228 2229 private GlobalConstants getGlobalConstants() { 2230 // Avoid hitting getContext() which might be costly for a non-Global unless needed. 2231 return GlobalConstants.GLOBAL_ONLY && !isGlobal() ? null : getContext().getGlobalConstants(); 2232 } 2233 2234 private GuardedInvocation createEmptySetMethod(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String strictErrorMessage, final boolean canBeFastScope) { 2235 final String name = NashornCallSiteDescriptor.getOperand(desc); 2236 if (NashornCallSiteDescriptor.isStrict(desc)) { 2237 throw typeError(strictErrorMessage, name, ScriptRuntime.safeToString(this)); 2238 } 2239 assert canBeFastScope || !NashornCallSiteDescriptor.isFastScope(desc); 2240 return new GuardedInvocation( 2241 Lookup.EMPTY_SETTER, 2242 NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), 2243 getProtoSwitchPoints(name, null), 2244 explicitInstanceOfCheck ? null : ClassCastException.class); 2245 } 2246 2247 @SuppressWarnings("unused") 2248 private boolean extensionCheck(final boolean isStrict, final String name) { 2249 if (isExtensible()) { 2250 return true; //go on and do the set. this is our guard 2251 } else if (isStrict) { 2252 //throw an error for attempting to do the set in strict mode 2253 throw typeError("object.non.extensible", name, ScriptRuntime.safeToString(this)); 2254 } else { 2255 //not extensible, non strict - this is a nop 2256 return false; 2257 } 2258 } 2259 2260 private GuardedInvocation findMegaMorphicSetMethod(final CallSiteDescriptor desc, final String name) { 2261 Context.getContextTrusted().getLogger(ObjectClassGenerator.class).warning("Megamorphic setter: ", desc, " ", name); 2262 final MethodType type = desc.getMethodType().insertParameterTypes(1, Object.class); 2263 //never bother with ClassCastExceptionGuard for megamorphic callsites 2264 final GuardedInvocation inv = findSetIndexMethod(getClass(), desc, false, type); 2265 return inv.replaceMethods(MH.insertArguments(inv.getInvocation(), 1, name), inv.getGuard()); 2266 } 2267 2268 @SuppressWarnings("unused") 2269 private static Object globalFilter(final Object object) { 2270 ScriptObject sobj = (ScriptObject) object; 2271 while (sobj != null && !(sobj instanceof Global)) { 2272 sobj = sobj.getProto(); 2273 } 2274 return sobj; 2275 } 2276 2277 /** 2278 * Lookup function for the set index method, available for subclasses as well, e.g. {@link NativeArray} 2279 * provides special quick accessor linkage for continuous arrays that are represented as Java arrays 2280 * 2281 * @param desc call site descriptor 2282 * @param request link request 2283 * 2284 * @return GuardedInvocation to be invoked at call site. 2285 */ 2286 protected GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value 2287 return findSetIndexMethod(getClass(), desc, explicitInstanceOfCheck(desc, request), desc.getMethodType()); 2288 } 2289 2290 /** 2291 * Find the appropriate SETINDEX method for an invoke dynamic call. 2292 * 2293 * @param clazz the receiver class 2294 * @param desc the call site descriptor 2295 * @param explicitInstanceOfCheck add an explicit instanceof check? 2296 * @param callType the method type at the call site 2297 * 2298 * @return GuardedInvocation to be invoked at call site. 2299 */ 2300 private static GuardedInvocation findSetIndexMethod(final Class<? extends ScriptObject> clazz, final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final MethodType callType) { 2301 assert callType.parameterCount() == 3; 2302 final Class<?> keyClass = callType.parameterType(1); 2303 final Class<?> valueClass = callType.parameterType(2); 2304 2305 MethodHandle methodHandle = findOwnMH_V(clazz, "set", void.class, keyClass, valueClass, int.class); 2306 methodHandle = MH.insertArguments(methodHandle, 3, NashornCallSiteDescriptor.getFlags(desc)); 2307 2308 return new GuardedInvocation(methodHandle, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class); 2309 } 2310 2311 /** 2312 * Fall back if a function property is not found. 2313 * @param desc The call site descriptor 2314 * @param request the link request 2315 * @return GuardedInvocation to be invoked at call site. 2316 */ 2317 public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) { 2318 final String name = NashornCallSiteDescriptor.getOperand(desc); 2319 final FindProperty find = findProperty(NO_SUCH_METHOD_NAME, true); 2320 final boolean scopeCall = isScope() && NashornCallSiteDescriptor.isScope(desc); 2321 2322 if (find == null) { 2323 return noSuchProperty(desc, request); 2324 } 2325 2326 final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request); 2327 2328 final Object value = find.getObjectValue(); 2329 if (!(value instanceof ScriptFunction)) { 2330 return createEmptyGetter(desc, explicitInstanceOfCheck, name); 2331 } 2332 2333 final ScriptFunction func = (ScriptFunction)value; 2334 final Object thiz = scopeCall && func.isStrict() ? UNDEFINED : this; 2335 // TODO: It'd be awesome if we could bind "name" without binding "this". 2336 // Since we're binding this we must use an identity guard here. 2337 return new GuardedInvocation( 2338 MH.dropArguments( 2339 MH.constant( 2340 ScriptFunction.class, 2341 func.createBound(thiz, new Object[] { name })), 2342 0, 2343 Object.class), 2344 NashornGuards.combineGuards( 2345 NashornGuards.getIdentityGuard(this), 2346 NashornGuards.getMapGuard(getMap(), true))); 2347 } 2348 2349 /** 2350 * Fall back if a property is not found. 2351 * @param desc the call site descriptor. 2352 * @param request the link request 2353 * @return GuardedInvocation to be invoked at call site. 2354 */ 2355 public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) { 2356 final String name = NashornCallSiteDescriptor.getOperand(desc); 2357 final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); 2358 final boolean scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc); 2359 2360 if (find != null) { 2361 final Object value = find.getObjectValue(); 2362 ScriptFunction func = null; 2363 MethodHandle mh = null; 2364 2365 if (value instanceof ScriptFunction) { 2366 func = (ScriptFunction)value; 2367 mh = getCallMethodHandle(func, desc.getMethodType(), name); 2368 } 2369 2370 if (mh != null) { 2371 assert func != null; 2372 if (scopeAccess && func.isStrict()) { 2373 mh = bindTo(mh, UNDEFINED); 2374 } 2375 2376 return new GuardedInvocation( 2377 mh, 2378 find.isSelf()? 2379 getKnownFunctionPropertyGuardSelf( 2380 getMap(), 2381 find.getGetter(Object.class, INVALID_PROGRAM_POINT, request), 2382 func) 2383 : 2384 //TODO this always does a scriptobject check 2385 getKnownFunctionPropertyGuardProto( 2386 getMap(), 2387 find.getGetter(Object.class, INVALID_PROGRAM_POINT, request), 2388 find.getProtoChainLength(), 2389 func), 2390 getProtoSwitchPoints(NO_SUCH_PROPERTY_NAME, find.getOwner()), 2391 //TODO this doesn't need a ClassCastException as guard always checks script object 2392 null); 2393 } 2394 } 2395 2396 if (scopeAccess) { 2397 throw referenceError("not.defined", name); 2398 } 2399 2400 return createEmptyGetter(desc, explicitInstanceOfCheck(desc, request), name); 2401 } 2402 2403 /** 2404 * Invoke fall back if a property is not found. 2405 * @param key Name of property. 2406 * @param isScope is this a scope access? 2407 * @param programPoint program point 2408 * @return Result from call. 2409 */ 2410 protected Object invokeNoSuchProperty(final Object key, final boolean isScope, final int programPoint) { 2411 final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); 2412 final Object func = (find != null)? find.getObjectValue() : null; 2413 2414 Object ret = UNDEFINED; 2415 if (func instanceof ScriptFunction) { 2416 final ScriptFunction sfunc = (ScriptFunction)func; 2417 final Object self = isScope && sfunc.isStrict()? UNDEFINED : this; 2418 ret = ScriptRuntime.apply(sfunc, self, key); 2419 } else if (isScope) { 2420 throw referenceError("not.defined", key.toString()); 2421 } 2422 2423 if (isValid(programPoint)) { 2424 throw new UnwarrantedOptimismException(ret, programPoint); 2425 } 2426 2427 return ret; 2428 } 2429 2430 2431 /** 2432 * Get __noSuchMethod__ as a function bound to this object and {@code name} if it is defined. 2433 * @param name the method name 2434 * @param isScope is this a scope access? 2435 * @return the bound function, or undefined 2436 */ 2437 private Object getNoSuchMethod(final String name, final boolean isScope, final int programPoint) { 2438 final FindProperty find = findProperty(NO_SUCH_METHOD_NAME, true); 2439 2440 if (find == null) { 2441 return invokeNoSuchProperty(name, isScope, programPoint); 2442 } 2443 2444 final Object value = find.getObjectValue(); 2445 if (!(value instanceof ScriptFunction)) { 2446 if (isScope) { 2447 throw referenceError("not.defined", name); 2448 } 2449 return UNDEFINED; 2450 } 2451 2452 final ScriptFunction func = (ScriptFunction)value; 2453 final Object self = isScope && func.isStrict()? UNDEFINED : this; 2454 return func.createBound(self, new Object[] {name}); 2455 } 2456 2457 private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) { 2458 if (NashornCallSiteDescriptor.isOptimistic(desc)) { 2459 throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT); 2460 } 2461 2462 return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()), 2463 NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoints(name, null), 2464 explicitInstanceOfCheck ? null : ClassCastException.class); 2465 } 2466 2467 private abstract static class ScriptObjectIterator <T extends Object> implements Iterator<T> { 2468 protected T[] values; 2469 protected final ScriptObject object; 2470 private int index; 2471 2472 ScriptObjectIterator(final ScriptObject object) { 2473 this.object = object; 2474 } 2475 2476 protected abstract void init(); 2477 2478 @Override 2479 public boolean hasNext() { 2480 if (values == null) { 2481 init(); 2482 } 2483 return index < values.length; 2484 } 2485 2486 @Override 2487 public T next() { 2488 if (values == null) { 2489 init(); 2490 } 2491 return values[index++]; 2492 } 2493 2494 @Override 2495 public void remove() { 2496 throw new UnsupportedOperationException("remove"); 2497 } 2498 } 2499 2500 private static class KeyIterator extends ScriptObjectIterator<String> { 2501 KeyIterator(final ScriptObject object) { 2502 super(object); 2503 } 2504 2505 @Override 2506 protected void init() { 2507 final Set<String> keys = new LinkedHashSet<>(); 2508 final Set<String> nonEnumerable = new HashSet<>(); 2509 for (ScriptObject self = object; self != null; self = self.getProto()) { 2510 keys.addAll(Arrays.asList(self.getOwnKeys(String.class, false, nonEnumerable))); 2511 } 2512 this.values = keys.toArray(new String[0]); 2513 } 2514 } 2515 2516 private static class ValueIterator extends ScriptObjectIterator<Object> { 2517 ValueIterator(final ScriptObject object) { 2518 super(object); 2519 } 2520 2521 @Override 2522 protected void init() { 2523 final ArrayList<Object> valueList = new ArrayList<>(); 2524 final Set<String> nonEnumerable = new HashSet<>(); 2525 for (ScriptObject self = object; self != null; self = self.getProto()) { 2526 for (final String key : self.getOwnKeys(String.class, false, nonEnumerable)) { 2527 valueList.add(self.get(key)); 2528 } 2529 } 2530 this.values = valueList.toArray(new Object[0]); 2531 } 2532 } 2533 2534 /** 2535 * Add a spill property for the given key. 2536 * @param key Property key. 2537 * @param flags Property flags. 2538 * @return Added property. 2539 */ 2540 private Property addSpillProperty(final Object key, final int flags, final Object value, final boolean hasInitialValue) { 2541 final PropertyMap propertyMap = getMap(); 2542 final int fieldSlot = propertyMap.getFreeFieldSlot(); 2543 final int propertyFlags = flags | (useDualFields() ? Property.DUAL_FIELDS : 0); 2544 2545 Property property; 2546 if (fieldSlot > -1) { 2547 property = hasInitialValue ? 2548 new AccessorProperty(key, propertyFlags, fieldSlot, this, value) : 2549 new AccessorProperty(key, propertyFlags, getClass(), fieldSlot); 2550 property = addOwnProperty(property); 2551 } else { 2552 final int spillSlot = propertyMap.getFreeSpillSlot(); 2553 property = hasInitialValue ? 2554 new SpillProperty(key, propertyFlags, spillSlot, this, value) : 2555 new SpillProperty(key, propertyFlags, spillSlot); 2556 property = addOwnProperty(property); 2557 ensureSpillSize(property.getSlot()); 2558 } 2559 return property; 2560 } 2561 2562 /** 2563 * Add a spill entry for the given key. 2564 * @param key Property key. 2565 * @return Setter method handle. 2566 */ 2567 MethodHandle addSpill(final Class<?> type, final String key) { 2568 return addSpillProperty(key, 0, null, false).getSetter(type, getMap()); 2569 } 2570 2571 /** 2572 * Make sure arguments are paired correctly, with respect to more parameters than declared, 2573 * fewer parameters than declared and other things that JavaScript allows. This might involve 2574 * creating collectors. 2575 * 2576 * @param methodHandle method handle for invoke 2577 * @param callType type of the call 2578 * 2579 * @return method handle with adjusted arguments 2580 */ 2581 protected static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType) { 2582 return pairArguments(methodHandle, callType, null); 2583 } 2584 2585 /** 2586 * Make sure arguments are paired correctly, with respect to more parameters than declared, 2587 * fewer parameters than declared and other things that JavaScript allows. This might involve 2588 * creating collectors. 2589 * 2590 * Make sure arguments are paired correctly. 2591 * @param methodHandle MethodHandle to adjust. 2592 * @param callType MethodType of the call site. 2593 * @param callerVarArg true if the caller is vararg, false otherwise, null if it should be inferred from the 2594 * {@code callType}; basically, if the last parameter type of the call site is an array, it'll be considered a 2595 * variable arity call site. These are ordinarily rare; Nashorn code generator creates variable arity call sites 2596 * when the call has more than {@link LinkerCallSite#ARGLIMIT} parameters. 2597 * 2598 * @return method handle with adjusted arguments 2599 */ 2600 public static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType, final Boolean callerVarArg) { 2601 final MethodType methodType = methodHandle.type(); 2602 if (methodType.equals(callType.changeReturnType(methodType.returnType()))) { 2603 return methodHandle; 2604 } 2605 2606 final int parameterCount = methodType.parameterCount(); 2607 final int callCount = callType.parameterCount(); 2608 2609 final boolean isCalleeVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray(); 2610 final boolean isCallerVarArg = callerVarArg != null ? callerVarArg : callCount > 0 && 2611 callType.parameterType(callCount - 1).isArray(); 2612 2613 if (isCalleeVarArg) { 2614 return isCallerVarArg ? 2615 methodHandle : 2616 MH.asCollector(methodHandle, Object[].class, callCount - parameterCount + 1); 2617 } 2618 2619 if (isCallerVarArg) { 2620 return adaptHandleToVarArgCallSite(methodHandle, callCount); 2621 } 2622 2623 if (callCount < parameterCount) { 2624 final int missingArgs = parameterCount - callCount; 2625 final Object[] fillers = new Object[missingArgs]; 2626 2627 Arrays.fill(fillers, UNDEFINED); 2628 2629 if (isCalleeVarArg) { 2630 fillers[missingArgs - 1] = ScriptRuntime.EMPTY_ARRAY; 2631 } 2632 2633 return MH.insertArguments( 2634 methodHandle, 2635 parameterCount - missingArgs, 2636 fillers); 2637 } 2638 2639 if (callCount > parameterCount) { 2640 final int discardedArgs = callCount - parameterCount; 2641 2642 final Class<?>[] discards = new Class<?>[discardedArgs]; 2643 Arrays.fill(discards, Object.class); 2644 2645 return MH.dropArguments(methodHandle, callCount - discardedArgs, discards); 2646 } 2647 2648 return methodHandle; 2649 } 2650 2651 static MethodHandle adaptHandleToVarArgCallSite(final MethodHandle mh, final int callSiteParamCount) { 2652 final int spreadArgs = mh.type().parameterCount() - callSiteParamCount + 1; 2653 return MH.filterArguments( 2654 MH.asSpreader( 2655 mh, 2656 Object[].class, 2657 spreadArgs), 2658 callSiteParamCount - 1, 2659 MH.insertArguments( 2660 TRUNCATINGFILTER, 2661 0, 2662 spreadArgs) 2663 ); 2664 } 2665 2666 @SuppressWarnings("unused") 2667 private static Object[] truncatingFilter(final int n, final Object[] array) { 2668 final int length = array == null ? 0 : array.length; 2669 if (n == length) { 2670 return array == null ? ScriptRuntime.EMPTY_ARRAY : array; 2671 } 2672 2673 final Object[] newArray = new Object[n]; 2674 2675 if (array != null) { 2676 System.arraycopy(array, 0, newArray, 0, Math.min(n, length)); 2677 } 2678 2679 if (length < n) { 2680 final Object fill = UNDEFINED; 2681 2682 for (int i = length; i < n; i++) { 2683 newArray[i] = fill; 2684 } 2685 } 2686 2687 return newArray; 2688 } 2689 2690 /** 2691 * Numeric length setter for length property 2692 * 2693 * @param newLength new length to set 2694 */ 2695 public final void setLength(final long newLength) { 2696 final ArrayData data = getArray(); 2697 final long arrayLength = data.length(); 2698 if (newLength == arrayLength) { 2699 return; 2700 } 2701 2702 if (newLength > arrayLength) { 2703 setArray(data.ensure(newLength - 1).safeDelete(arrayLength, newLength - 1, false)); 2704 return; 2705 } 2706 2707 if (newLength < arrayLength) { 2708 long actualLength = newLength; 2709 2710 // Check for numeric keys in property map and delete them or adjust length, depending on whether 2711 // they're defined as configurable. See ES5 #15.4.5.2 2712 if (getMap().containsArrayKeys()) { 2713 2714 for (long l = arrayLength - 1; l >= newLength; l--) { 2715 final FindProperty find = findProperty(JSType.toString(l), false); 2716 2717 if (find != null) { 2718 2719 if (find.getProperty().isConfigurable()) { 2720 deleteOwnProperty(find.getProperty()); 2721 } else { 2722 actualLength = l + 1; 2723 break; 2724 } 2725 } 2726 } 2727 } 2728 2729 setArray(data.shrink(actualLength)); 2730 data.setLength(actualLength); 2731 } 2732 } 2733 2734 private int getInt(final int index, final Object key, final int programPoint) { 2735 if (isValidArrayIndex(index)) { 2736 for (ScriptObject object = this; ; ) { 2737 if (object.getMap().containsArrayKeys()) { 2738 final FindProperty find = object.findProperty(key, false); 2739 2740 if (find != null) { 2741 return getIntValue(find, programPoint); 2742 } 2743 } 2744 2745 if ((object = object.getProto()) == null) { 2746 break; 2747 } 2748 2749 final ArrayData array = object.getArray(); 2750 2751 if (array.has(index)) { 2752 return isValid(programPoint) ? 2753 array.getIntOptimistic(index, programPoint) : 2754 array.getInt(index); 2755 } 2756 } 2757 } else { 2758 final FindProperty find = findProperty(key, true); 2759 2760 if (find != null) { 2761 return getIntValue(find, programPoint); 2762 } 2763 } 2764 2765 return JSType.toInt32(invokeNoSuchProperty(key, false, programPoint)); 2766 } 2767 2768 @Override 2769 public int getInt(final Object key, final int programPoint) { 2770 final Object primitiveKey = JSType.toPrimitive(key, String.class); 2771 final int index = getArrayIndex(primitiveKey); 2772 final ArrayData array = getArray(); 2773 2774 if (array.has(index)) { 2775 return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index); 2776 } 2777 2778 return getInt(index, JSType.toPropertyKey(primitiveKey), programPoint); 2779 } 2780 2781 @Override 2782 public int getInt(final double key, final int programPoint) { 2783 final int index = getArrayIndex(key); 2784 final ArrayData array = getArray(); 2785 2786 if (array.has(index)) { 2787 return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index); 2788 } 2789 2790 return getInt(index, JSType.toString(key), programPoint); 2791 } 2792 2793 @Override 2794 public int getInt(final int key, final int programPoint) { 2795 final int index = getArrayIndex(key); 2796 final ArrayData array = getArray(); 2797 2798 if (array.has(index)) { 2799 return isValid(programPoint) ? array.getIntOptimistic(key, programPoint) : array.getInt(key); 2800 } 2801 2802 return getInt(index, JSType.toString(key), programPoint); 2803 } 2804 2805 private double getDouble(final int index, final Object key, final int programPoint) { 2806 if (isValidArrayIndex(index)) { 2807 for (ScriptObject object = this; ; ) { 2808 if (object.getMap().containsArrayKeys()) { 2809 final FindProperty find = object.findProperty(key, false); 2810 if (find != null) { 2811 return getDoubleValue(find, programPoint); 2812 } 2813 } 2814 2815 if ((object = object.getProto()) == null) { 2816 break; 2817 } 2818 2819 final ArrayData array = object.getArray(); 2820 2821 if (array.has(index)) { 2822 return isValid(programPoint) ? 2823 array.getDoubleOptimistic(index, programPoint) : 2824 array.getDouble(index); 2825 } 2826 } 2827 } else { 2828 final FindProperty find = findProperty(key, true); 2829 2830 if (find != null) { 2831 return getDoubleValue(find, programPoint); 2832 } 2833 } 2834 2835 return JSType.toNumber(invokeNoSuchProperty(key, false, INVALID_PROGRAM_POINT)); 2836 } 2837 2838 @Override 2839 public double getDouble(final Object key, final int programPoint) { 2840 final Object primitiveKey = JSType.toPrimitive(key, String.class); 2841 final int index = getArrayIndex(primitiveKey); 2842 final ArrayData array = getArray(); 2843 2844 if (array.has(index)) { 2845 return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index); 2846 } 2847 2848 return getDouble(index, JSType.toPropertyKey(primitiveKey), programPoint); 2849 } 2850 2851 @Override 2852 public double getDouble(final double key, final int programPoint) { 2853 final int index = getArrayIndex(key); 2854 final ArrayData array = getArray(); 2855 2856 if (array.has(index)) { 2857 return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index); 2858 } 2859 2860 return getDouble(index, JSType.toString(key), programPoint); 2861 } 2862 2863 @Override 2864 public double getDouble(final int key, final int programPoint) { 2865 final int index = getArrayIndex(key); 2866 final ArrayData array = getArray(); 2867 2868 if (array.has(index)) { 2869 return isValid(programPoint) ? array.getDoubleOptimistic(key, programPoint) : array.getDouble(key); 2870 } 2871 2872 return getDouble(index, JSType.toString(key), programPoint); 2873 } 2874 2875 private Object get(final int index, final Object key) { 2876 if (isValidArrayIndex(index)) { 2877 for (ScriptObject object = this; ; ) { 2878 if (object.getMap().containsArrayKeys()) { 2879 final FindProperty find = object.findProperty(key, false); 2880 2881 if (find != null) { 2882 return find.getObjectValue(); 2883 } 2884 } 2885 2886 if ((object = object.getProto()) == null) { 2887 break; 2888 } 2889 2890 final ArrayData array = object.getArray(); 2891 2892 if (array.has(index)) { 2893 return array.getObject(index); 2894 } 2895 } 2896 } else { 2897 final FindProperty find = findProperty(key, true); 2898 2899 if (find != null) { 2900 return find.getObjectValue(); 2901 } 2902 } 2903 2904 return invokeNoSuchProperty(key, false, INVALID_PROGRAM_POINT); 2905 } 2906 2907 @Override 2908 public Object get(final Object key) { 2909 final Object primitiveKey = JSType.toPrimitive(key, String.class); 2910 final int index = getArrayIndex(primitiveKey); 2911 final ArrayData array = getArray(); 2912 2913 if (array.has(index)) { 2914 return array.getObject(index); 2915 } 2916 2917 return get(index, JSType.toPropertyKey(primitiveKey)); 2918 } 2919 2920 @Override 2921 public Object get(final double key) { 2922 final int index = getArrayIndex(key); 2923 final ArrayData array = getArray(); 2924 2925 if (array.has(index)) { 2926 return array.getObject(index); 2927 } 2928 2929 return get(index, JSType.toString(key)); 2930 } 2931 2932 @Override 2933 public Object get(final int key) { 2934 final int index = getArrayIndex(key); 2935 final ArrayData array = getArray(); 2936 2937 if (array.has(index)) { 2938 return array.getObject(index); 2939 } 2940 2941 return get(index, JSType.toString(key)); 2942 } 2943 2944 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final int value, final int callSiteFlags) { 2945 if (getMap().containsArrayKeys()) { 2946 final String key = JSType.toString(longIndex); 2947 final FindProperty find = findProperty(key, true); 2948 if (find != null) { 2949 setObject(find, callSiteFlags, key, value); 2950 return true; 2951 } 2952 } 2953 return false; 2954 } 2955 2956 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final long value, final int callSiteFlags) { 2957 if (getMap().containsArrayKeys()) { 2958 final String key = JSType.toString(longIndex); 2959 final FindProperty find = findProperty(key, true); 2960 if (find != null) { 2961 setObject(find, callSiteFlags, key, value); 2962 return true; 2963 } 2964 } 2965 return false; 2966 } 2967 2968 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final double value, final int callSiteFlags) { 2969 if (getMap().containsArrayKeys()) { 2970 final String key = JSType.toString(longIndex); 2971 final FindProperty find = findProperty(key, true); 2972 if (find != null) { 2973 setObject(find, callSiteFlags, key, value); 2974 return true; 2975 } 2976 } 2977 return false; 2978 } 2979 2980 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final Object value, final int callSiteFlags) { 2981 if (getMap().containsArrayKeys()) { 2982 final String key = JSType.toString(longIndex); 2983 final FindProperty find = findProperty(key, true); 2984 if (find != null) { 2985 setObject(find, callSiteFlags, key, value); 2986 return true; 2987 } 2988 } 2989 return false; 2990 } 2991 2992 //value agnostic 2993 private boolean doesNotHaveEnsureLength(final long longIndex, final long oldLength, final int callSiteFlags) { 2994 if (longIndex >= oldLength) { 2995 if (!isExtensible()) { 2996 if (isStrictFlag(callSiteFlags)) { 2997 throw typeError("object.non.extensible", JSType.toString(longIndex), ScriptRuntime.safeToString(this)); 2998 } 2999 return true; 3000 } 3001 setArray(getArray().ensure(longIndex)); 3002 } 3003 return false; 3004 } 3005 3006 private void doesNotHave(final int index, final int value, final int callSiteFlags) { 3007 final long oldLength = getArray().length(); 3008 final long longIndex = ArrayIndex.toLongIndex(index); 3009 if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) { 3010 final boolean strict = isStrictFlag(callSiteFlags); 3011 setArray(getArray().set(index, value, strict).safeDelete(oldLength, longIndex - 1, strict)); 3012 } 3013 } 3014 3015 private void doesNotHave(final int index, final double value, final int callSiteFlags) { 3016 final long oldLength = getArray().length(); 3017 final long longIndex = ArrayIndex.toLongIndex(index); 3018 if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) { 3019 final boolean strict = isStrictFlag(callSiteFlags); 3020 setArray(getArray().set(index, value, strict).safeDelete(oldLength, longIndex - 1, strict)); 3021 } 3022 } 3023 3024 private void doesNotHave(final int index, final Object value, final int callSiteFlags) { 3025 final long oldLength = getArray().length(); 3026 final long longIndex = ArrayIndex.toLongIndex(index); 3027 if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) { 3028 final boolean strict = isStrictFlag(callSiteFlags); 3029 setArray(getArray().set(index, value, strict).safeDelete(oldLength, longIndex - 1, strict)); 3030 } 3031 } 3032 3033 /** 3034 * This is the most generic of all Object setters. Most of the others use this in some form. 3035 * TODO: should be further specialized 3036 * 3037 * @param find found property 3038 * @param callSiteFlags callsite flags 3039 * @param key property key 3040 * @param value property value 3041 */ 3042 public final void setObject(final FindProperty find, final int callSiteFlags, final Object key, final Object value) { 3043 FindProperty f = find; 3044 3045 invalidateGlobalConstant(key); 3046 3047 if (f != null && f.isInheritedOrdinaryProperty()) { 3048 final boolean isScope = isScopeFlag(callSiteFlags); 3049 // If the start object of the find is not this object it means the property was found inside a 3050 // 'with' statement expression (see WithObject.findProperty()). In this case we forward the 'set' 3051 // to the 'with' object. 3052 // Note that although a 'set' operation involving a with statement follows scope rules outside 3053 // the 'with' expression (the 'set' operation is performed on the owning prototype if it exists), 3054 // it follows non-scope rules inside the 'with' expression (set is performed on the top level object). 3055 // This is why we clear the callsite flags and FindProperty in the forward call to the 'with' object. 3056 if (isScope && f.getSelf() != this) { 3057 f.getSelf().setObject(null, 0, key, value); 3058 return; 3059 } 3060 // Setting a property should not modify the property in prototype unless this is a scope callsite 3061 // and the owner is a scope object as well (with the exception of 'with' statement handled above). 3062 if (!isScope || !f.getOwner().isScope()) { 3063 f = null; 3064 } 3065 } 3066 3067 if (f != null) { 3068 if (!f.getProperty().isWritable() || !f.getProperty().hasNativeSetter()) { 3069 if (isScopeFlag(callSiteFlags) && f.getProperty().isLexicalBinding()) { 3070 throw typeError("assign.constant", key.toString()); // Overwriting ES6 const should throw also in non-strict mode. 3071 } 3072 if (isStrictFlag(callSiteFlags)) { 3073 throw typeError( 3074 f.getProperty().isAccessorProperty() ? "property.has.no.setter" : "property.not.writable", 3075 key.toString(), ScriptRuntime.safeToString(this)); 3076 } 3077 return; 3078 } 3079 3080 f.setValue(value, isStrictFlag(callSiteFlags)); 3081 3082 } else if (!isExtensible()) { 3083 if (isStrictFlag(callSiteFlags)) { 3084 throw typeError("object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 3085 } 3086 } else { 3087 ScriptObject sobj = this; 3088 // undefined scope properties are set in the global object. 3089 if (isScope()) { 3090 while (sobj != null && !(sobj instanceof Global)) { 3091 sobj = sobj.getProto(); 3092 } 3093 assert sobj != null : "no parent global object in scope"; 3094 } 3095 //this will unbox any Number object to its primitive type in case the 3096 //property supports primitive types, so it doesn't matter that it comes 3097 //in as an Object. 3098 sobj.addSpillProperty(key, 0, value, true); 3099 } 3100 } 3101 3102 @Override 3103 public void set(final Object key, final int value, final int callSiteFlags) { 3104 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3105 final int index = getArrayIndex(primitiveKey); 3106 3107 if (isValidArrayIndex(index)) { 3108 final ArrayData data = getArray(); 3109 if (data.has(index)) { 3110 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3111 } else { 3112 doesNotHave(index, value, callSiteFlags); 3113 } 3114 3115 return; 3116 } 3117 3118 final Object propName = JSType.toPropertyKey(primitiveKey); 3119 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3120 } 3121 3122 @Override 3123 public void set(final Object key, final double value, final int callSiteFlags) { 3124 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3125 final int index = getArrayIndex(primitiveKey); 3126 3127 if (isValidArrayIndex(index)) { 3128 final ArrayData data = getArray(); 3129 if (data.has(index)) { 3130 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3131 } else { 3132 doesNotHave(index, value, callSiteFlags); 3133 } 3134 3135 return; 3136 } 3137 3138 final Object propName = JSType.toPropertyKey(primitiveKey); 3139 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3140 } 3141 3142 @Override 3143 public void set(final Object key, final Object value, final int callSiteFlags) { 3144 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3145 final int index = getArrayIndex(primitiveKey); 3146 3147 if (isValidArrayIndex(index)) { 3148 final ArrayData data = getArray(); 3149 if (data.has(index)) { 3150 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3151 } else { 3152 doesNotHave(index, value, callSiteFlags); 3153 } 3154 3155 return; 3156 } 3157 3158 final Object propName = JSType.toPropertyKey(primitiveKey); 3159 setObject(findProperty(propName, true), callSiteFlags, propName, value); 3160 } 3161 3162 @Override 3163 public void set(final double key, final int value, final int callSiteFlags) { 3164 final int index = getArrayIndex(key); 3165 3166 if (isValidArrayIndex(index)) { 3167 final ArrayData data = getArray(); 3168 if (data.has(index)) { 3169 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3170 } else { 3171 doesNotHave(index, value, callSiteFlags); 3172 } 3173 3174 return; 3175 } 3176 3177 final String propName = JSType.toString(key); 3178 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3179 } 3180 3181 @Override 3182 public void set(final double key, final double value, final int callSiteFlags) { 3183 final int index = getArrayIndex(key); 3184 3185 if (isValidArrayIndex(index)) { 3186 final ArrayData data = getArray(); 3187 if (data.has(index)) { 3188 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3189 } else { 3190 doesNotHave(index, value, callSiteFlags); 3191 } 3192 3193 return; 3194 } 3195 3196 final String propName = JSType.toString(key); 3197 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3198 } 3199 3200 @Override 3201 public void set(final double key, final Object value, final int callSiteFlags) { 3202 final int index = getArrayIndex(key); 3203 3204 if (isValidArrayIndex(index)) { 3205 final ArrayData data = getArray(); 3206 if (data.has(index)) { 3207 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3208 } else { 3209 doesNotHave(index, value, callSiteFlags); 3210 } 3211 3212 return; 3213 } 3214 3215 final String propName = JSType.toString(key); 3216 setObject(findProperty(propName, true), callSiteFlags, propName, value); 3217 } 3218 3219 @Override 3220 public void set(final int key, final int value, final int callSiteFlags) { 3221 final int index = getArrayIndex(key); 3222 if (isValidArrayIndex(index)) { 3223 if (getArray().has(index)) { 3224 final ArrayData data = getArray(); 3225 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3226 } else { 3227 doesNotHave(index, value, callSiteFlags); 3228 } 3229 return; 3230 } 3231 3232 final String propName = JSType.toString(key); 3233 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3234 } 3235 3236 @Override 3237 public void set(final int key, final double value, final int callSiteFlags) { 3238 final int index = getArrayIndex(key); 3239 3240 if (isValidArrayIndex(index)) { 3241 final ArrayData data = getArray(); 3242 if (data.has(index)) { 3243 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3244 } else { 3245 doesNotHave(index, value, callSiteFlags); 3246 } 3247 3248 return; 3249 } 3250 3251 final String propName = JSType.toString(key); 3252 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3253 } 3254 3255 @Override 3256 public void set(final int key, final Object value, final int callSiteFlags) { 3257 final int index = getArrayIndex(key); 3258 3259 if (isValidArrayIndex(index)) { 3260 final ArrayData data = getArray(); 3261 if (data.has(index)) { 3262 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3263 } else { 3264 doesNotHave(index, value, callSiteFlags); 3265 } 3266 3267 return; 3268 } 3269 3270 final String propName = JSType.toString(key); 3271 setObject(findProperty(propName, true), callSiteFlags, propName, value); 3272 } 3273 3274 @Override 3275 public boolean has(final Object key) { 3276 final Object primitiveKey = JSType.toPrimitive(key); 3277 final int index = getArrayIndex(primitiveKey); 3278 return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toPropertyKey(primitiveKey), true); 3279 } 3280 3281 @Override 3282 public boolean has(final double key) { 3283 final int index = getArrayIndex(key); 3284 return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true); 3285 } 3286 3287 @Override 3288 public boolean has(final int key) { 3289 final int index = getArrayIndex(key); 3290 return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true); 3291 } 3292 3293 private boolean hasArrayProperty(final int index) { 3294 boolean hasArrayKeys = false; 3295 3296 for (ScriptObject self = this; self != null; self = self.getProto()) { 3297 if (self.getArray().has(index)) { 3298 return true; 3299 } 3300 hasArrayKeys = hasArrayKeys || self.getMap().containsArrayKeys(); 3301 } 3302 3303 return hasArrayKeys && hasProperty(ArrayIndex.toKey(index), true); 3304 } 3305 3306 @Override 3307 public boolean hasOwnProperty(final Object key) { 3308 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3309 final int index = getArrayIndex(primitiveKey); 3310 return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toPropertyKey(primitiveKey), false); 3311 } 3312 3313 @Override 3314 public boolean hasOwnProperty(final int key) { 3315 final int index = getArrayIndex(key); 3316 return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false); 3317 } 3318 3319 @Override 3320 public boolean hasOwnProperty(final double key) { 3321 final int index = getArrayIndex(key); 3322 return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false); 3323 } 3324 3325 private boolean hasOwnArrayProperty(final int index) { 3326 return getArray().has(index) || getMap().containsArrayKeys() && hasProperty(ArrayIndex.toKey(index), false); 3327 } 3328 3329 @Override 3330 public boolean delete(final int key, final boolean strict) { 3331 final int index = getArrayIndex(key); 3332 final ArrayData array = getArray(); 3333 3334 if (array.has(index)) { 3335 if (array.canDelete(index, strict)) { 3336 setArray(array.delete(index)); 3337 return true; 3338 } 3339 return false; 3340 } 3341 return deleteObject(JSType.toObject(key), strict); 3342 } 3343 3344 @Override 3345 public boolean delete(final double key, final boolean strict) { 3346 final int index = getArrayIndex(key); 3347 final ArrayData array = getArray(); 3348 3349 if (array.has(index)) { 3350 if (array.canDelete(index, strict)) { 3351 setArray(array.delete(index)); 3352 return true; 3353 } 3354 return false; 3355 } 3356 3357 return deleteObject(JSType.toObject(key), strict); 3358 } 3359 3360 @Override 3361 public boolean delete(final Object key, final boolean strict) { 3362 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3363 final int index = getArrayIndex(primitiveKey); 3364 final ArrayData array = getArray(); 3365 3366 if (array.has(index)) { 3367 if (array.canDelete(index, strict)) { 3368 setArray(array.delete(index)); 3369 return true; 3370 } 3371 return false; 3372 } 3373 3374 return deleteObject(primitiveKey, strict); 3375 } 3376 3377 private boolean deleteObject(final Object key, final boolean strict) { 3378 final Object propName = JSType.toPropertyKey(key); 3379 final FindProperty find = findProperty(propName, false); 3380 3381 if (find == null) { 3382 return true; 3383 } 3384 3385 if (!find.getProperty().isConfigurable()) { 3386 if (strict) { 3387 throw typeError("cant.delete.property", propName.toString(), ScriptRuntime.safeToString(this)); 3388 } 3389 return false; 3390 } 3391 3392 final Property prop = find.getProperty(); 3393 deleteOwnProperty(prop); 3394 3395 return true; 3396 } 3397 3398 /** 3399 * Return a shallow copy of this ScriptObject. 3400 * @return a shallow copy. 3401 */ 3402 public final ScriptObject copy() { 3403 try { 3404 return clone(); 3405 } catch (final CloneNotSupportedException e) { 3406 throw new RuntimeException(e); 3407 } 3408 } 3409 3410 @Override 3411 protected ScriptObject clone() throws CloneNotSupportedException { 3412 final ScriptObject clone = (ScriptObject) super.clone(); 3413 if (objectSpill != null) { 3414 clone.objectSpill = objectSpill.clone(); 3415 if (primitiveSpill != null) { 3416 clone.primitiveSpill = primitiveSpill.clone(); 3417 } 3418 } 3419 clone.arrayData = arrayData.copy(); 3420 return clone; 3421 } 3422 3423 /** 3424 * Make a new UserAccessorProperty property. getter and setter functions are stored in 3425 * this ScriptObject and slot values are used in property object. 3426 * 3427 * @param key the property name 3428 * @param propertyFlags attribute flags of the property 3429 * @param getter getter function for the property 3430 * @param setter setter function for the property 3431 * @return the newly created UserAccessorProperty 3432 */ 3433 protected final UserAccessorProperty newUserAccessors(final Object key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 3434 final UserAccessorProperty uc = getMap().newUserAccessors(key, propertyFlags); 3435 //property.getSetter(Object.class, getMap()); 3436 uc.setAccessors(this, getMap(), new UserAccessorProperty.Accessors(getter, setter)); 3437 return uc; 3438 } 3439 3440 /** 3441 * Returns {@code true} if properties for this object should use dual field mode, {@code false} otherwise. 3442 * @return {@code true} if dual fields should be used. 3443 */ 3444 protected boolean useDualFields() { 3445 return !StructureLoader.isSingleFieldStructure(getClass().getName()); 3446 } 3447 3448 Object ensureSpillSize(final int slot) { 3449 final int oldLength = objectSpill == null ? 0 : objectSpill.length; 3450 if (slot < oldLength) { 3451 return this; 3452 } 3453 final int newLength = alignUp(slot + 1, SPILL_RATE); 3454 final Object[] newObjectSpill = new Object[newLength]; 3455 final long[] newPrimitiveSpill = useDualFields() ? new long[newLength] : null; 3456 3457 if (objectSpill != null) { 3458 System.arraycopy(objectSpill, 0, newObjectSpill, 0, oldLength); 3459 if (primitiveSpill != null && newPrimitiveSpill != null) { 3460 System.arraycopy(primitiveSpill, 0, newPrimitiveSpill, 0, oldLength); 3461 } 3462 } 3463 3464 this.primitiveSpill = newPrimitiveSpill; 3465 this.objectSpill = newObjectSpill; 3466 3467 return this; 3468 } 3469 3470 private static MethodHandle findOwnMH_V(final Class<? extends ScriptObject> clazz, final String name, final Class<?> rtype, final Class<?>... types) { 3471 // TODO: figure out how can it work for NativeArray$Prototype etc. 3472 return MH.findVirtual(MethodHandles.lookup(), ScriptObject.class, name, MH.type(rtype, types)); 3473 } 3474 3475 private static MethodHandle findOwnMH_V(final String name, final Class<?> rtype, final Class<?>... types) { 3476 return findOwnMH_V(ScriptObject.class, name, rtype, types); 3477 } 3478 3479 private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { 3480 return MH.findStatic(MethodHandles.lookup(), ScriptObject.class, name, MH.type(rtype, types)); 3481 } 3482 3483 private static MethodHandle getKnownFunctionPropertyGuardSelf(final PropertyMap map, final MethodHandle getter, final ScriptFunction func) { 3484 return MH.insertArguments(KNOWNFUNCPROPGUARDSELF, 1, map, getter, func); 3485 } 3486 3487 @SuppressWarnings("unused") 3488 private static boolean knownFunctionPropertyGuardSelf(final Object self, final PropertyMap map, final MethodHandle getter, final ScriptFunction func) { 3489 if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) { 3490 try { 3491 return getter.invokeExact(self) == func; 3492 } catch (final RuntimeException | Error e) { 3493 throw e; 3494 } catch (final Throwable t) { 3495 throw new RuntimeException(t); 3496 } 3497 } 3498 3499 return false; 3500 } 3501 3502 private static MethodHandle getKnownFunctionPropertyGuardProto(final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) { 3503 return MH.insertArguments(KNOWNFUNCPROPGUARDPROTO, 1, map, getter, depth, func); 3504 } 3505 3506 private static ScriptObject getProto(final ScriptObject self, final int depth) { 3507 ScriptObject proto = self; 3508 for (int d = 0; d < depth; d++) { 3509 proto = proto.getProto(); 3510 if (proto == null) { 3511 return null; 3512 } 3513 } 3514 3515 return proto; 3516 } 3517 3518 @SuppressWarnings("unused") 3519 private static boolean knownFunctionPropertyGuardProto(final Object self, final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) { 3520 if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) { 3521 final ScriptObject proto = getProto((ScriptObject)self, depth); 3522 if (proto == null) { 3523 return false; 3524 } 3525 try { 3526 return getter.invokeExact((Object)proto) == func; 3527 } catch (final RuntimeException | Error e) { 3528 throw e; 3529 } catch (final Throwable t) { 3530 throw new RuntimeException(t); 3531 } 3532 } 3533 3534 return false; 3535 } 3536 3537 /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created */ 3538 private static LongAdder count; 3539 3540 static { 3541 if (Context.DEBUG) { 3542 count = new LongAdder(); 3543 } 3544 } 3545 /** 3546 * Get number of {@code ScriptObject} instances created. If not running in debug 3547 * mode this is always 0 3548 * 3549 * @return number of ScriptObjects created 3550 */ 3551 public static long getCount() { 3552 return count.longValue(); 3553 } 3554} 3555