ScriptObject.java revision 1626:d99fa86747ee
1/* 2 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.runtime; 27 28import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; 29import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCall; 30import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup; 31import static jdk.nashorn.internal.lookup.Lookup.MH; 32import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; 33import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 34import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_DOUBLE; 35import static jdk.nashorn.internal.runtime.JSType.UNDEFINED_INT; 36import static jdk.nashorn.internal.runtime.PropertyDescriptor.CONFIGURABLE; 37import static jdk.nashorn.internal.runtime.PropertyDescriptor.ENUMERABLE; 38import static jdk.nashorn.internal.runtime.PropertyDescriptor.GET; 39import static jdk.nashorn.internal.runtime.PropertyDescriptor.SET; 40import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE; 41import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE; 42import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 43import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT; 44import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid; 45import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.getArrayIndex; 46import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex; 47import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.isScopeFlag; 48import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.isStrictFlag; 49import static jdk.nashorn.internal.runtime.linker.NashornGuards.explicitInstanceOfCheck; 50 51import java.lang.invoke.MethodHandle; 52import java.lang.invoke.MethodHandles; 53import java.lang.invoke.MethodType; 54import java.lang.invoke.SwitchPoint; 55import java.lang.reflect.Array; 56import java.util.AbstractMap; 57import java.util.ArrayList; 58import java.util.Arrays; 59import java.util.Collection; 60import java.util.Collections; 61import java.util.HashSet; 62import java.util.Iterator; 63import java.util.LinkedHashSet; 64import java.util.List; 65import java.util.Map; 66import java.util.Set; 67import java.util.concurrent.atomic.LongAdder; 68import jdk.dynalink.CallSiteDescriptor; 69import jdk.dynalink.NamedOperation; 70import jdk.dynalink.StandardOperation; 71import jdk.dynalink.linker.GuardedInvocation; 72import jdk.dynalink.linker.LinkRequest; 73import jdk.nashorn.internal.codegen.CompilerConstants.Call; 74import jdk.nashorn.internal.codegen.ObjectClassGenerator; 75import jdk.nashorn.internal.codegen.types.Type; 76import jdk.nashorn.internal.lookup.Lookup; 77import jdk.nashorn.internal.objects.AccessorPropertyDescriptor; 78import jdk.nashorn.internal.objects.DataPropertyDescriptor; 79import jdk.nashorn.internal.objects.Global; 80import jdk.nashorn.internal.objects.NativeArray; 81import jdk.nashorn.internal.runtime.arrays.ArrayData; 82import jdk.nashorn.internal.runtime.arrays.ArrayIndex; 83import jdk.nashorn.internal.runtime.linker.Bootstrap; 84import jdk.nashorn.internal.runtime.linker.LinkerCallSite; 85import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor; 86import jdk.nashorn.internal.runtime.linker.NashornGuards; 87 88/** 89 * Base class for generic JavaScript objects. 90 * <p> 91 * Notes: 92 * <ul> 93 * <li>The map is used to identify properties in the object.</li> 94 * <li>If the map is modified then it must be cloned and replaced. This notifies 95 * any code that made assumptions about the object that things have changed. 96 * Ex. CallSites that have been validated must check to see if the map has 97 * changed (or a map from a different object type) and hence relink the method 98 * to call.</li> 99 * <li>Modifications of the map include adding/deleting attributes or changing a 100 * function field value.</li> 101 * </ul> 102 */ 103 104public abstract class ScriptObject implements PropertyAccess, Cloneable { 105 /** __proto__ special property name inside object literals. ES6 draft. */ 106 public static final String PROTO_PROPERTY_NAME = "__proto__"; 107 108 /** Search fall back routine name for "no such method" */ 109 public static final String NO_SUCH_METHOD_NAME = "__noSuchMethod__"; 110 111 /** Search fall back routine name for "no such property" */ 112 public static final String NO_SUCH_PROPERTY_NAME = "__noSuchProperty__"; 113 114 /** Per ScriptObject flag - is this an array object? */ 115 public static final int IS_ARRAY = 1 << 0; 116 117 /** Per ScriptObject flag - is this an arguments object? */ 118 public static final int IS_ARGUMENTS = 1 << 1; 119 120 /** Is length property not-writable? */ 121 public static final int IS_LENGTH_NOT_WRITABLE = 1 << 2; 122 123 /** Is this a builtin object? */ 124 public static final int IS_BUILTIN = 1 << 3; 125 126 /** 127 * Spill growth rate - by how many elements does {@link ScriptObject#primitiveSpill} and 128 * {@link ScriptObject#objectSpill} when full 129 */ 130 public static final int SPILL_RATE = 8; 131 132 /** Map to property information and accessor functions. Ordered by insertion. */ 133 private PropertyMap map; 134 135 /** objects proto. */ 136 private ScriptObject proto; 137 138 /** Object flags. */ 139 private int flags; 140 141 /** Area for primitive properties added to object after instantiation, see {@link AccessorProperty} */ 142 protected long[] primitiveSpill; 143 144 /** Area for reference properties added to object after instantiation, see {@link AccessorProperty} */ 145 protected Object[] objectSpill; 146 147 /** Indexed array data. */ 148 private ArrayData arrayData; 149 150 /** Method handle to retrieve prototype of this object */ 151 public static final MethodHandle GETPROTO = findOwnMH_V("getProto", ScriptObject.class); 152 153 static final MethodHandle MEGAMORPHIC_GET = findOwnMH_V("megamorphicGet", Object.class, String.class, boolean.class, boolean.class); 154 static final MethodHandle GLOBALFILTER = findOwnMH_S("globalFilter", Object.class, Object.class); 155 static final MethodHandle DECLARE_AND_SET = findOwnMH_V("declareAndSet", void.class, String.class, Object.class); 156 157 private static final MethodHandle TRUNCATINGFILTER = findOwnMH_S("truncatingFilter", Object[].class, int.class, Object[].class); 158 private static final MethodHandle KNOWNFUNCPROPGUARDSELF = findOwnMH_S("knownFunctionPropertyGuardSelf", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, ScriptFunction.class); 159 private static final MethodHandle KNOWNFUNCPROPGUARDPROTO = findOwnMH_S("knownFunctionPropertyGuardProto", boolean.class, Object.class, PropertyMap.class, MethodHandle.class, int.class, ScriptFunction.class); 160 161 private static final ArrayList<MethodHandle> PROTO_FILTERS = new ArrayList<>(); 162 163 /** Method handle for getting the array data */ 164 public static final Call GET_ARRAY = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArray", ArrayData.class); 165 166 /** Method handle for getting a function argument at a given index. Used from MapCreator */ 167 public static final Call GET_ARGUMENT = virtualCall(MethodHandles.lookup(), ScriptObject.class, "getArgument", Object.class, int.class); 168 169 /** Method handle for setting a function argument at a given index. Used from MapCreator */ 170 public static final Call SET_ARGUMENT = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setArgument", void.class, int.class, Object.class); 171 172 /** Method handle for getting the proto of a ScriptObject */ 173 public static final Call GET_PROTO = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class); 174 175 /** Method handle for getting the proto of a ScriptObject */ 176 public static final Call GET_PROTO_DEPTH = virtualCallNoLookup(ScriptObject.class, "getProto", ScriptObject.class, int.class); 177 178 /** Method handle for setting the proto of a ScriptObject */ 179 public static final Call SET_GLOBAL_OBJECT_PROTO = staticCallNoLookup(ScriptObject.class, "setGlobalObjectProto", void.class, ScriptObject.class); 180 181 /** Method handle for setting the proto of a ScriptObject after checking argument */ 182 public static final Call SET_PROTO_FROM_LITERAL = virtualCallNoLookup(ScriptObject.class, "setProtoFromLiteral", void.class, Object.class); 183 184 /** Method handle for setting the user accessors of a ScriptObject */ 185 //TODO fastpath this 186 public static final Call SET_USER_ACCESSORS = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class); 187 188 static final MethodHandle[] SET_SLOW = new MethodHandle[] { 189 findOwnMH_V("set", void.class, Object.class, int.class, int.class), 190 findOwnMH_V("set", void.class, Object.class, double.class, int.class), 191 findOwnMH_V("set", void.class, Object.class, Object.class, int.class) 192 }; 193 194 /** Method handle to reset the map of this ScriptObject */ 195 public static final Call SET_MAP = virtualCallNoLookup(ScriptObject.class, "setMap", void.class, PropertyMap.class); 196 197 static final MethodHandle CAS_MAP = findOwnMH_V("compareAndSetMap", boolean.class, PropertyMap.class, PropertyMap.class); 198 static final MethodHandle EXTENSION_CHECK = findOwnMH_V("extensionCheck", boolean.class, boolean.class, String.class); 199 static final MethodHandle ENSURE_SPILL_SIZE = findOwnMH_V("ensureSpillSize", Object.class, int.class); 200 201 /** 202 * Constructor 203 */ 204 public ScriptObject() { 205 this(null); 206 } 207 208 /** 209 * Constructor 210 * 211 * @param map {@link PropertyMap} used to create the initial object 212 */ 213 public ScriptObject(final PropertyMap map) { 214 if (Context.DEBUG) { 215 ScriptObject.count.increment(); 216 } 217 this.arrayData = ArrayData.EMPTY_ARRAY; 218 this.setMap(map == null ? PropertyMap.newMap() : map); 219 } 220 221 /** 222 * Constructor that directly sets the prototype to {@code proto} and property map to 223 * {@code map} without invalidating the map as calling {@link #setProto(ScriptObject)} 224 * would do. This should only be used for objects that are always constructed with the 225 * same combination of prototype and property map. 226 * 227 * @param proto the prototype object 228 * @param map initial {@link PropertyMap} 229 */ 230 protected ScriptObject(final ScriptObject proto, final PropertyMap map) { 231 this(map); 232 this.proto = proto; 233 } 234 235 /** 236 * Constructor used to instantiate spill properties directly. Used from 237 * SpillObjectCreator. 238 * 239 * @param map property maps 240 * @param primitiveSpill primitive spills 241 * @param objectSpill reference spills 242 */ 243 public ScriptObject(final PropertyMap map, final long[] primitiveSpill, final Object[] objectSpill) { 244 this(map); 245 this.primitiveSpill = primitiveSpill; 246 this.objectSpill = objectSpill; 247 assert primitiveSpill == null || primitiveSpill.length == objectSpill.length : " primitive spill pool size is not the same length as object spill pool size"; 248 } 249 250 /** 251 * Check whether this is a global object 252 * @return true if global 253 */ 254 protected boolean isGlobal() { 255 return false; 256 } 257 258 private static int alignUp(final int size, final int alignment) { 259 return size + alignment - 1 & ~(alignment - 1); 260 } 261 262 /** 263 * Given a number of properties, return the aligned to SPILL_RATE 264 * buffer size required for the smallest spill pool needed to 265 * house them 266 * @param nProperties number of properties 267 * @return property buffer length, a multiple of SPILL_RATE 268 */ 269 public static int spillAllocationLength(final int nProperties) { 270 return alignUp(nProperties, SPILL_RATE); 271 } 272 273 /** 274 * Copy all properties from the source object with their receiver bound to the source. 275 * This function was known as mergeMap 276 * 277 * @param source The source object to copy from. 278 */ 279 public void addBoundProperties(final ScriptObject source) { 280 addBoundProperties(source, source.getMap().getProperties()); 281 } 282 283 /** 284 * Copy all properties from the array with their receiver bound to the source. 285 * 286 * @param source The source object to copy from. 287 * @param properties The array of properties to copy. 288 */ 289 public void addBoundProperties(final ScriptObject source, final Property[] properties) { 290 PropertyMap newMap = this.getMap(); 291 final boolean extensible = newMap.isExtensible(); 292 293 for (final Property property : properties) { 294 newMap = addBoundProperty(newMap, source, property, extensible); 295 } 296 297 this.setMap(newMap); 298 } 299 300 /** 301 * Add a bound property from {@code source}, using the interim property map {@code propMap}, and return the 302 * new interim property map. 303 * 304 * @param propMap the property map 305 * @param source the source object 306 * @param property the property to be added 307 * @param extensible whether the current object is extensible or not 308 * @return the new property map 309 */ 310 protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final Property property, final boolean extensible) { 311 PropertyMap newMap = propMap; 312 final Object key = property.getKey(); 313 final Property oldProp = newMap.findProperty(key); 314 if (oldProp == null) { 315 if (! extensible) { 316 throw typeError("object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 317 } 318 319 if (property instanceof UserAccessorProperty) { 320 // Note: we copy accessor functions to this object which is semantically different from binding. 321 final UserAccessorProperty prop = this.newUserAccessors(key, property.getFlags(), property.getGetterFunction(source), property.getSetterFunction(source)); 322 newMap = newMap.addPropertyNoHistory(prop); 323 } else { 324 newMap = newMap.addPropertyBind((AccessorProperty)property, source); 325 } 326 } else { 327 // See ECMA section 10.5 Declaration Binding Instantiation 328 // step 5 processing each function declaration. 329 if (property.isFunctionDeclaration() && !oldProp.isConfigurable()) { 330 if (oldProp instanceof UserAccessorProperty || 331 !(oldProp.isWritable() && oldProp.isEnumerable())) { 332 throw typeError("cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 333 } 334 } 335 } 336 return newMap; 337 } 338 339 /** 340 * Copy all properties from the array with their receiver bound to the source. 341 * 342 * @param source The source object to copy from. 343 * @param properties The collection of accessor properties to copy. 344 */ 345 public void addBoundProperties(final Object source, final AccessorProperty[] properties) { 346 PropertyMap newMap = this.getMap(); 347 final boolean extensible = newMap.isExtensible(); 348 349 for (final AccessorProperty property : properties) { 350 final Object key = property.getKey(); 351 352 if (newMap.findProperty(key) == null) { 353 if (! extensible) { 354 throw typeError("object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 355 } 356 newMap = newMap.addPropertyBind(property, source); 357 } 358 } 359 360 this.setMap(newMap); 361 } 362 363 /** 364 * Bind the method handle to the specified receiver, while preserving its original type (it will just ignore the 365 * first argument in lieu of the bound argument). 366 * @param methodHandle Method handle to bind to. 367 * @param receiver Object to bind. 368 * @return Bound method handle. 369 */ 370 static MethodHandle bindTo(final MethodHandle methodHandle, final Object receiver) { 371 return MH.dropArguments(MH.bindTo(methodHandle, receiver), 0, methodHandle.type().parameterType(0)); 372 } 373 374 /** 375 * Return a property iterator. 376 * @return Property iterator. 377 */ 378 public Iterator<String> propertyIterator() { 379 return new KeyIterator(this); 380 } 381 382 /** 383 * Return a property value iterator. 384 * @return Property value iterator. 385 */ 386 public Iterator<Object> valueIterator() { 387 return new ValueIterator(this); 388 } 389 390 /** 391 * ECMA 8.10.1 IsAccessorDescriptor ( Desc ) 392 * @return true if this has a {@link AccessorPropertyDescriptor} with a getter or a setter 393 */ 394 public final boolean isAccessorDescriptor() { 395 return has(GET) || has(SET); 396 } 397 398 /** 399 * ECMA 8.10.2 IsDataDescriptor ( Desc ) 400 * @return true if this has a {@link DataPropertyDescriptor}, i.e. the object has a property value and is writable 401 */ 402 public final boolean isDataDescriptor() { 403 return has(VALUE) || has(WRITABLE); 404 } 405 406 /** 407 * ECMA 8.10.5 ToPropertyDescriptor ( Obj ) 408 * 409 * @return property descriptor 410 */ 411 public final PropertyDescriptor toPropertyDescriptor() { 412 final Global global = Context.getGlobal(); 413 414 final PropertyDescriptor desc; 415 if (isDataDescriptor()) { 416 if (has(SET) || has(GET)) { 417 throw typeError(global, "inconsistent.property.descriptor"); 418 } 419 420 desc = global.newDataDescriptor(UNDEFINED, false, false, false); 421 } else if (isAccessorDescriptor()) { 422 if (has(VALUE) || has(WRITABLE)) { 423 throw typeError(global, "inconsistent.property.descriptor"); 424 } 425 426 desc = global.newAccessorDescriptor(UNDEFINED, UNDEFINED, false, false); 427 } else { 428 desc = global.newGenericDescriptor(false, false); 429 } 430 431 return desc.fillFrom(this); 432 } 433 434 /** 435 * ECMA 8.10.5 ToPropertyDescriptor ( Obj ) 436 * 437 * @param global global scope object 438 * @param obj object to create property descriptor from 439 * 440 * @return property descriptor 441 */ 442 public static PropertyDescriptor toPropertyDescriptor(final Global global, final Object obj) { 443 if (obj instanceof ScriptObject) { 444 return ((ScriptObject)obj).toPropertyDescriptor(); 445 } 446 447 throw typeError(global, "not.an.object", ScriptRuntime.safeToString(obj)); 448 } 449 450 /** 451 * ECMA 8.12.1 [[GetOwnProperty]] (P) 452 * 453 * @param key property key 454 * 455 * @return Returns the Property Descriptor of the named own property of this 456 * object, or undefined if absent. 457 */ 458 public Object getOwnPropertyDescriptor(final Object key) { 459 final Property property = getMap().findProperty(key); 460 461 final Global global = Context.getGlobal(); 462 463 if (property != null) { 464 final ScriptFunction get = property.getGetterFunction(this); 465 final ScriptFunction set = property.getSetterFunction(this); 466 467 final boolean configurable = property.isConfigurable(); 468 final boolean enumerable = property.isEnumerable(); 469 final boolean writable = property.isWritable(); 470 471 if (property.isAccessorProperty()) { 472 return global.newAccessorDescriptor( 473 get != null ? 474 get : 475 UNDEFINED, 476 set != null ? 477 set : 478 UNDEFINED, 479 configurable, 480 enumerable); 481 } 482 483 return global.newDataDescriptor(getWithProperty(property), configurable, enumerable, writable); 484 } 485 486 final int index = getArrayIndex(key); 487 final ArrayData array = getArray(); 488 489 if (array.has(index)) { 490 return array.getDescriptor(global, index); 491 } 492 493 return UNDEFINED; 494 } 495 496 /** 497 * ECMA 8.12.2 [[GetProperty]] (P) 498 * 499 * @param key property key 500 * 501 * @return Returns the fully populated Property Descriptor of the named property 502 * of this object, or undefined if absent. 503 */ 504 public Object getPropertyDescriptor(final String key) { 505 final Object res = getOwnPropertyDescriptor(key); 506 507 if (res != UNDEFINED) { 508 return res; 509 } else if (getProto() != null) { 510 return getProto().getOwnPropertyDescriptor(key); 511 } else { 512 return UNDEFINED; 513 } 514 } 515 516 /** 517 * Invalidate any existing global constant method handles that may exist for {@code key}. 518 * @param key the property name 519 */ 520 protected void invalidateGlobalConstant(final Object key) { 521 final GlobalConstants globalConstants = getGlobalConstants(); 522 if (globalConstants != null) { 523 globalConstants.delete(key); 524 } 525 } 526 527 /** 528 * ECMA 8.12.9 [[DefineOwnProperty]] (P, Desc, Throw) 529 * 530 * @param key the property key 531 * @param propertyDesc the property descriptor 532 * @param reject is the property extensible - true means new definitions are rejected 533 * 534 * @return true if property was successfully defined 535 */ 536 public boolean defineOwnProperty(final Object key, final Object propertyDesc, final boolean reject) { 537 final Global global = Context.getGlobal(); 538 final PropertyDescriptor desc = toPropertyDescriptor(global, propertyDesc); 539 final Object current = getOwnPropertyDescriptor(key); 540 541 invalidateGlobalConstant(key); 542 543 if (current == UNDEFINED) { 544 if (isExtensible()) { 545 // add a new own property 546 addOwnProperty(key, desc); 547 return true; 548 } 549 // new property added to non-extensible object 550 if (reject) { 551 throw typeError(global, "object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 552 } 553 return false; 554 } 555 556 // modifying an existing property 557 final PropertyDescriptor currentDesc = (PropertyDescriptor)current; 558 final PropertyDescriptor newDesc = desc; 559 560 if (newDesc.type() == PropertyDescriptor.GENERIC && !newDesc.has(CONFIGURABLE) && !newDesc.has(ENUMERABLE)) { 561 // every descriptor field is absent 562 return true; 563 } 564 565 if (newDesc.hasAndEquals(currentDesc)) { 566 // every descriptor field of the new is same as the current 567 return true; 568 } 569 570 if (!currentDesc.isConfigurable()) { 571 if (newDesc.has(CONFIGURABLE) && newDesc.isConfigurable()) { 572 // not configurable can not be made configurable 573 if (reject) { 574 throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 575 } 576 return false; 577 } 578 579 if (newDesc.has(ENUMERABLE) && 580 currentDesc.isEnumerable() != newDesc.isEnumerable()) { 581 // cannot make non-enumerable as enumerable or vice-versa 582 if (reject) { 583 throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 584 } 585 return false; 586 } 587 } 588 589 int propFlags = Property.mergeFlags(currentDesc, newDesc); 590 Property property = getMap().findProperty(key); 591 592 if (currentDesc.type() == PropertyDescriptor.DATA && 593 (newDesc.type() == PropertyDescriptor.DATA || 594 newDesc.type() == PropertyDescriptor.GENERIC)) { 595 if (!currentDesc.isConfigurable() && !currentDesc.isWritable()) { 596 if (newDesc.has(WRITABLE) && newDesc.isWritable() || 597 newDesc.has(VALUE) && !ScriptRuntime.sameValue(currentDesc.getValue(), newDesc.getValue())) { 598 if (reject) { 599 throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 600 } 601 return false; 602 } 603 } 604 605 final boolean newValue = newDesc.has(VALUE); 606 final Object value = newValue ? newDesc.getValue() : currentDesc.getValue(); 607 608 if (newValue && property != null) { 609 // Temporarily clear flags. 610 property = modifyOwnProperty(property, 0); 611 set(key, value, 0); 612 //this might change the map if we change types of the property 613 //hence we need to read it again. note that we should probably 614 //have the setter return the new property throughout and in 615 //general respect Property return values from modify and add 616 //functions - which we don't seem to do at all here :-( 617 //There is already a bug filed to generify PropertyAccess so we 618 //can have the setter return e.g. a Property 619 property = getMap().findProperty(key); 620 } 621 622 if (property == null) { 623 // promoting an arrayData value to actual property 624 addOwnProperty(key, propFlags, value); 625 checkIntegerKey(key); 626 } else { 627 // Now set the new flags 628 modifyOwnProperty(property, propFlags); 629 } 630 } else if (currentDesc.type() == PropertyDescriptor.ACCESSOR && 631 (newDesc.type() == PropertyDescriptor.ACCESSOR || 632 newDesc.type() == PropertyDescriptor.GENERIC)) { 633 if (!currentDesc.isConfigurable()) { 634 if (newDesc.has(PropertyDescriptor.GET) && !ScriptRuntime.sameValue(currentDesc.getGetter(), newDesc.getGetter()) || 635 newDesc.has(PropertyDescriptor.SET) && !ScriptRuntime.sameValue(currentDesc.getSetter(), newDesc.getSetter())) { 636 if (reject) { 637 throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 638 } 639 return false; 640 } 641 } 642 // New set the new features. 643 modifyOwnProperty(property, propFlags, 644 newDesc.has(GET) ? newDesc.getGetter() : currentDesc.getGetter(), 645 newDesc.has(SET) ? newDesc.getSetter() : currentDesc.getSetter()); 646 } else { 647 // changing descriptor type 648 if (!currentDesc.isConfigurable()) { 649 // not configurable can not be made configurable 650 if (reject) { 651 throw typeError(global, "cant.redefine.property", key.toString(), ScriptRuntime.safeToString(this)); 652 } 653 return false; 654 } 655 656 propFlags = 0; 657 658 // Preserve only configurable and enumerable from current desc 659 // if those are not overridden in the new property descriptor. 660 boolean value = newDesc.has(CONFIGURABLE) ? newDesc.isConfigurable() : currentDesc.isConfigurable(); 661 if (!value) { 662 propFlags |= Property.NOT_CONFIGURABLE; 663 } 664 value = newDesc.has(ENUMERABLE)? newDesc.isEnumerable() : currentDesc.isEnumerable(); 665 if (!value) { 666 propFlags |= Property.NOT_ENUMERABLE; 667 } 668 669 final int type = newDesc.type(); 670 if (type == PropertyDescriptor.DATA) { 671 // get writable from the new descriptor 672 value = newDesc.has(WRITABLE) && newDesc.isWritable(); 673 if (!value) { 674 propFlags |= Property.NOT_WRITABLE; 675 } 676 677 // delete the old property 678 deleteOwnProperty(property); 679 // add new data property 680 addOwnProperty(key, propFlags, newDesc.getValue()); 681 } else if (type == PropertyDescriptor.ACCESSOR) { 682 if (property == null) { 683 addOwnProperty(key, propFlags, 684 newDesc.has(GET) ? newDesc.getGetter() : null, 685 newDesc.has(SET) ? newDesc.getSetter() : null); 686 } else { 687 // Modify old property with the new features. 688 modifyOwnProperty(property, propFlags, 689 newDesc.has(GET) ? newDesc.getGetter() : null, 690 newDesc.has(SET) ? newDesc.getSetter() : null); 691 } 692 } 693 } 694 695 checkIntegerKey(key); 696 697 return true; 698 } 699 700 /** 701 * Almost like defineOwnProperty(int,Object) for arrays this one does 702 * not add 'gap' elements (like the array one does). 703 * 704 * @param index key for property 705 * @param value value to define 706 */ 707 public void defineOwnProperty(final int index, final Object value) { 708 assert isValidArrayIndex(index) : "invalid array index"; 709 final long longIndex = ArrayIndex.toLongIndex(index); 710 final long oldLength = getArray().length(); 711 if (longIndex >= oldLength) { 712 setArray(getArray().ensure(longIndex).safeDelete(oldLength, longIndex - 1, false)); 713 } 714 setArray(getArray().set(index, value, false)); 715 } 716 717 private void checkIntegerKey(final Object key) { 718 final int index = getArrayIndex(key); 719 720 if (isValidArrayIndex(index)) { 721 final ArrayData data = getArray(); 722 723 if (data.has(index)) { 724 setArray(data.delete(index)); 725 } 726 } 727 } 728 729 /** 730 * Add a new property to the object. 731 * 732 * @param key property key 733 * @param propertyDesc property descriptor for property 734 */ 735 public final void addOwnProperty(final Object key, final PropertyDescriptor propertyDesc) { 736 // Already checked that there is no own property with that key. 737 PropertyDescriptor pdesc = propertyDesc; 738 739 final int propFlags = Property.toFlags(pdesc); 740 741 if (pdesc.type() == PropertyDescriptor.GENERIC) { 742 final Global global = Context.getGlobal(); 743 final PropertyDescriptor dDesc = global.newDataDescriptor(UNDEFINED, false, false, false); 744 745 dDesc.fillFrom((ScriptObject)pdesc); 746 pdesc = dDesc; 747 } 748 749 final int type = pdesc.type(); 750 if (type == PropertyDescriptor.DATA) { 751 addOwnProperty(key, propFlags, pdesc.getValue()); 752 } else if (type == PropertyDescriptor.ACCESSOR) { 753 addOwnProperty(key, propFlags, 754 pdesc.has(GET) ? pdesc.getGetter() : null, 755 pdesc.has(SET) ? pdesc.getSetter() : null); 756 } 757 758 checkIntegerKey(key); 759 } 760 761 /** 762 * Low level property API (not using property descriptors) 763 * <p> 764 * Find a property in the prototype hierarchy. Note: this is final and not 765 * a good idea to override. If you have to, use 766 * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or 767 * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the 768 * overriding way to find array properties 769 * 770 * @see jdk.nashorn.internal.objects.NativeArray 771 * 772 * @param key Property key. 773 * @param deep Whether the search should look up proto chain. 774 * 775 * @return FindPropertyData or null if not found. 776 */ 777 public final FindProperty findProperty(final Object key, final boolean deep) { 778 return findProperty(key, deep, this); 779 } 780 781 /** 782 * Low level property API (not using property descriptors) 783 * <p> 784 * Find a property in the prototype hierarchy. Note: this is not a good idea 785 * to override except as it was done in {@link WithObject}. 786 * If you have to, use 787 * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or 788 * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the 789 * overriding way to find array properties 790 * 791 * @see jdk.nashorn.internal.objects.NativeArray 792 * 793 * @param key Property key. 794 * @param deep Whether the search should look up proto chain. 795 * @param start the object on which the lookup was originally initiated 796 * 797 * @return FindPropertyData or null if not found. 798 */ 799 protected FindProperty findProperty(final Object key, final boolean deep, final ScriptObject start) { 800 801 final PropertyMap selfMap = getMap(); 802 final Property property = selfMap.findProperty(key); 803 804 if (property != null) { 805 return new FindProperty(start, this, property); 806 } 807 808 if (deep) { 809 final ScriptObject myProto = getProto(); 810 final FindProperty find = myProto == null ? null : myProto.findProperty(key, true, start); 811 // checkSharedProtoMap must be invoked after myProto.checkSharedProtoMap to propagate 812 // shared proto invalidation up the prototype chain. It also must be invoked when prototype is null. 813 checkSharedProtoMap(); 814 return find; 815 } 816 817 return null; 818 } 819 820 /** 821 * Low level property API. This is similar to {@link #findProperty(Object, boolean)} but returns a 822 * {@code boolean} value instead of a {@link FindProperty} object. 823 * @param key Property key. 824 * @param deep Whether the search should look up proto chain. 825 * @return true if the property was found. 826 */ 827 boolean hasProperty(final Object key, final boolean deep) { 828 if (getMap().findProperty(key) != null) { 829 return true; 830 } 831 832 if (deep) { 833 final ScriptObject myProto = getProto(); 834 if (myProto != null) { 835 return myProto.hasProperty(key, true); 836 } 837 } 838 839 return false; 840 } 841 842 private SwitchPoint findBuiltinSwitchPoint(final Object key) { 843 for (ScriptObject myProto = getProto(); myProto != null; myProto = myProto.getProto()) { 844 final Property prop = myProto.getMap().findProperty(key); 845 if (prop != null) { 846 final SwitchPoint sp = prop.getBuiltinSwitchPoint(); 847 if (sp != null && !sp.hasBeenInvalidated()) { 848 return sp; 849 } 850 } 851 } 852 return null; 853 } 854 855 /** 856 * Add a new property to the object. 857 * <p> 858 * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s 859 * 860 * @param key Property key. 861 * @param propertyFlags Property flags. 862 * @param getter Property getter, or null if not defined 863 * @param setter Property setter, or null if not defined 864 * 865 * @return New property. 866 */ 867 public final Property addOwnProperty(final Object key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 868 return addOwnProperty(newUserAccessors(key, propertyFlags, getter, setter)); 869 } 870 871 /** 872 * Add a new property to the object. 873 * <p> 874 * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s 875 * 876 * @param key Property key. 877 * @param propertyFlags Property flags. 878 * @param value Value of property 879 * 880 * @return New property. 881 */ 882 public final Property addOwnProperty(final Object key, final int propertyFlags, final Object value) { 883 return addSpillProperty(key, propertyFlags, value, true); 884 } 885 886 /** 887 * Add a new property to the object. 888 * <p> 889 * This a more "low level" way that doesn't involve {@link PropertyDescriptor}s 890 * 891 * @param newProperty property to add 892 * 893 * @return New property. 894 */ 895 public final Property addOwnProperty(final Property newProperty) { 896 PropertyMap oldMap = getMap(); 897 while (true) { 898 final PropertyMap newMap = oldMap.addProperty(newProperty); 899 if (!compareAndSetMap(oldMap, newMap)) { 900 oldMap = getMap(); 901 final Property oldProperty = oldMap.findProperty(newProperty.getKey()); 902 903 if (oldProperty != null) { 904 return oldProperty; 905 } 906 } else { 907 return newProperty; 908 } 909 } 910 } 911 912 private void erasePropertyValue(final Property property) { 913 // Erase the property field value with undefined. If the property is an accessor property 914 // we don't want to call the setter!! 915 if (property != null && !property.isAccessorProperty()) { 916 property.setValue(this, this, UNDEFINED, false); 917 } 918 } 919 920 /** 921 * Delete a property from the object. 922 * 923 * @param property Property to delete. 924 * 925 * @return true if deleted. 926 */ 927 public final boolean deleteOwnProperty(final Property property) { 928 erasePropertyValue(property); 929 PropertyMap oldMap = getMap(); 930 931 while (true) { 932 final PropertyMap newMap = oldMap.deleteProperty(property); 933 934 if (newMap == null) { 935 return false; 936 } 937 938 if (!compareAndSetMap(oldMap, newMap)) { 939 oldMap = getMap(); 940 } else { 941 // delete getter and setter function references so that we don't leak 942 if (property instanceof UserAccessorProperty) { 943 ((UserAccessorProperty)property).setAccessors(this, getMap(), null); 944 } 945 946 invalidateGlobalConstant(property.getKey()); 947 return true; 948 } 949 } 950 951 } 952 953 /** 954 * Fast initialization functions for ScriptFunctions that are strict, to avoid 955 * creating setters that probably aren't used. Inject directly into the spill pool 956 * the defaults for "arguments" and "caller" 957 * 958 * @param key property key 959 * @param propertyFlags flags 960 * @param getter getter for {@link UserAccessorProperty}, null if not present or N/A 961 * @param setter setter for {@link UserAccessorProperty}, null if not present or N/A 962 */ 963 protected final void initUserAccessors(final String key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 964 final PropertyMap oldMap = getMap(); 965 final int slot = oldMap.getFreeSpillSlot(); 966 ensureSpillSize(slot); 967 objectSpill[slot] = new UserAccessorProperty.Accessors(getter, setter); 968 Property newProperty; 969 PropertyMap newMap; 970 do { 971 newProperty = new UserAccessorProperty(key, propertyFlags, slot); 972 newMap = oldMap.addProperty(newProperty); 973 } while (!compareAndSetMap(oldMap, newMap)); 974 } 975 976 /** 977 * Modify a property in the object 978 * 979 * @param oldProperty property to modify 980 * @param propertyFlags new property flags 981 * @param getter getter for {@link UserAccessorProperty}, null if not present or N/A 982 * @param setter setter for {@link UserAccessorProperty}, null if not present or N/A 983 * 984 * @return new property 985 */ 986 public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 987 Property newProperty; 988 989 if (oldProperty instanceof UserAccessorProperty) { 990 final UserAccessorProperty uc = (UserAccessorProperty)oldProperty; 991 final int slot = uc.getSlot(); 992 993 assert uc.getLocalType() == Object.class; 994 final UserAccessorProperty.Accessors gs = uc.getAccessors(this); //this crashes 995 assert gs != null; 996 //reuse existing getter setter for speed 997 gs.set(getter, setter); 998 if (uc.getFlags() == (propertyFlags | Property.IS_ACCESSOR_PROPERTY)) { 999 return oldProperty; 1000 } 1001 newProperty = new UserAccessorProperty(uc.getKey(), propertyFlags, slot); 1002 } else { 1003 // erase old property value and create new user accessor property 1004 erasePropertyValue(oldProperty); 1005 newProperty = newUserAccessors(oldProperty.getKey(), propertyFlags, getter, setter); 1006 } 1007 1008 return modifyOwnProperty(oldProperty, newProperty); 1009 } 1010 1011 /** 1012 * Modify a property in the object 1013 * 1014 * @param oldProperty property to modify 1015 * @param propertyFlags new property flags 1016 * 1017 * @return new property 1018 */ 1019 public final Property modifyOwnProperty(final Property oldProperty, final int propertyFlags) { 1020 return modifyOwnProperty(oldProperty, oldProperty.setFlags(propertyFlags)); 1021 } 1022 1023 /** 1024 * Modify a property in the object, replacing a property with a new one 1025 * 1026 * @param oldProperty property to replace 1027 * @param newProperty property to replace it with 1028 * 1029 * @return new property 1030 */ 1031 private Property modifyOwnProperty(final Property oldProperty, final Property newProperty) { 1032 if (oldProperty == newProperty) { 1033 return newProperty; //nop 1034 } 1035 1036 assert newProperty.getKey().equals(oldProperty.getKey()) : "replacing property with different key"; 1037 1038 PropertyMap oldMap = getMap(); 1039 1040 while (true) { 1041 final PropertyMap newMap = oldMap.replaceProperty(oldProperty, newProperty); 1042 1043 if (!compareAndSetMap(oldMap, newMap)) { 1044 oldMap = getMap(); 1045 final Property oldPropertyLookup = oldMap.findProperty(oldProperty.getKey()); 1046 1047 if (oldPropertyLookup != null && oldPropertyLookup.equals(newProperty)) { 1048 return oldPropertyLookup; 1049 } 1050 } else { 1051 return newProperty; 1052 } 1053 } 1054 } 1055 1056 /** 1057 * Update getter and setter in an object literal. 1058 * 1059 * @param key Property key. 1060 * @param getter {@link UserAccessorProperty} defined getter, or null if none 1061 * @param setter {@link UserAccessorProperty} defined setter, or null if none 1062 */ 1063 public final void setUserAccessors(final String key, final ScriptFunction getter, final ScriptFunction setter) { 1064 final Property oldProperty = getMap().findProperty(key); 1065 if (oldProperty instanceof UserAccessorProperty) { 1066 modifyOwnProperty(oldProperty, oldProperty.getFlags(), getter, setter); 1067 } else { 1068 addOwnProperty(newUserAccessors(key, oldProperty != null ? oldProperty.getFlags() : 0, getter, setter)); 1069 } 1070 } 1071 1072 private static int getIntValue(final FindProperty find, final int programPoint) { 1073 final MethodHandle getter = find.getGetter(int.class, programPoint, null); 1074 if (getter != null) { 1075 try { 1076 return (int)getter.invokeExact((Object)find.getGetterReceiver()); 1077 } catch (final Error|RuntimeException e) { 1078 throw e; 1079 } catch (final Throwable e) { 1080 throw new RuntimeException(e); 1081 } 1082 } 1083 1084 return UNDEFINED_INT; 1085 } 1086 1087 private static double getDoubleValue(final FindProperty find, final int programPoint) { 1088 final MethodHandle getter = find.getGetter(double.class, programPoint, null); 1089 if (getter != null) { 1090 try { 1091 return (double)getter.invokeExact((Object)find.getGetterReceiver()); 1092 } catch (final Error|RuntimeException e) { 1093 throw e; 1094 } catch (final Throwable e) { 1095 throw new RuntimeException(e); 1096 } 1097 } 1098 1099 return UNDEFINED_DOUBLE; 1100 } 1101 1102 /** 1103 * Return methodHandle of value function for call. 1104 * 1105 * @param find data from find property. 1106 * @param type method type of function. 1107 * @param bindName null or name to bind to second argument (property not found method.) 1108 * 1109 * @return value of property as a MethodHandle or null. 1110 */ 1111 protected MethodHandle getCallMethodHandle(final FindProperty find, final MethodType type, final String bindName) { 1112 return getCallMethodHandle(find.getObjectValue(), type, bindName); 1113 } 1114 1115 /** 1116 * Return methodHandle of value function for call. 1117 * 1118 * @param value value of receiver, it not a {@link ScriptFunction} this will return null. 1119 * @param type method type of function. 1120 * @param bindName null or name to bind to second argument (property not found method.) 1121 * 1122 * @return value of property as a MethodHandle or null. 1123 */ 1124 protected static MethodHandle getCallMethodHandle(final Object value, final MethodType type, final String bindName) { 1125 return value instanceof ScriptFunction ? ((ScriptFunction)value).getCallMethodHandle(type, bindName) : null; 1126 } 1127 1128 /** 1129 * Get value using found property. 1130 * 1131 * @param property Found property. 1132 * 1133 * @return Value of property. 1134 */ 1135 public final Object getWithProperty(final Property property) { 1136 return new FindProperty(this, this, property).getObjectValue(); 1137 } 1138 1139 /** 1140 * Get a property given a key 1141 * 1142 * @param key property key 1143 * 1144 * @return property for key 1145 */ 1146 public final Property getProperty(final String key) { 1147 return getMap().findProperty(key); 1148 } 1149 1150 /** 1151 * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) 1152 * Used for argument access in a vararg function using parameter name. 1153 * Returns the argument at a given key (index) 1154 * 1155 * @param key argument index 1156 * 1157 * @return the argument at the given position, or undefined if not present 1158 */ 1159 public Object getArgument(final int key) { 1160 return get(key); 1161 } 1162 1163 /** 1164 * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) 1165 * Used for argument access in a vararg function using parameter name. 1166 * Returns the argument at a given key (index) 1167 * 1168 * @param key argument index 1169 * @param value the value to write at the given index 1170 */ 1171 public void setArgument(final int key, final Object value) { 1172 set(key, value, 0); 1173 } 1174 1175 /** 1176 * Return the current context from the object's map. 1177 * @return Current context. 1178 */ 1179 protected Context getContext() { 1180 return Context.fromClass(getClass()); 1181 } 1182 1183 /** 1184 * Return the map of an object. 1185 * @return PropertyMap object. 1186 */ 1187 public final PropertyMap getMap() { 1188 return map; 1189 } 1190 1191 /** 1192 * Set the initial map. 1193 * @param map Initial map. 1194 */ 1195 public final void setMap(final PropertyMap map) { 1196 this.map = map; 1197 } 1198 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); 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); 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, 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.isInherited() && !find.getProperty().isAccessorProperty()) { 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 final MethodType type = desc.getMethodType().insertParameterTypes(1, Object.class); 2262 //never bother with ClassCastExceptionGuard for megamorphic callsites 2263 final GuardedInvocation inv = findSetIndexMethod(getClass(), desc, false, type); 2264 return inv.replaceMethods(MH.insertArguments(inv.getInvocation(), 1, name), inv.getGuard()); 2265 } 2266 2267 @SuppressWarnings("unused") 2268 private static Object globalFilter(final Object object) { 2269 ScriptObject sobj = (ScriptObject) object; 2270 while (sobj != null && !(sobj instanceof Global)) { 2271 sobj = sobj.getProto(); 2272 } 2273 return sobj; 2274 } 2275 2276 /** 2277 * Lookup function for the set index method, available for subclasses as well, e.g. {@link NativeArray} 2278 * provides special quick accessor linkage for continuous arrays that are represented as Java arrays 2279 * 2280 * @param desc call site descriptor 2281 * @param request link request 2282 * 2283 * @return GuardedInvocation to be invoked at call site. 2284 */ 2285 protected GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value 2286 return findSetIndexMethod(getClass(), desc, explicitInstanceOfCheck(desc, request), desc.getMethodType()); 2287 } 2288 2289 /** 2290 * Find the appropriate SETINDEX method for an invoke dynamic call. 2291 * 2292 * @param clazz the receiver class 2293 * @param desc the call site descriptor 2294 * @param explicitInstanceOfCheck add an explicit instanceof check? 2295 * @param callType the method type at the call site 2296 * 2297 * @return GuardedInvocation to be invoked at call site. 2298 */ 2299 private static GuardedInvocation findSetIndexMethod(final Class<? extends ScriptObject> clazz, final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final MethodType callType) { 2300 assert callType.parameterCount() == 3; 2301 final Class<?> keyClass = callType.parameterType(1); 2302 final Class<?> valueClass = callType.parameterType(2); 2303 2304 MethodHandle methodHandle = findOwnMH_V(clazz, "set", void.class, keyClass, valueClass, int.class); 2305 methodHandle = MH.insertArguments(methodHandle, 3, NashornCallSiteDescriptor.getFlags(desc)); 2306 2307 return new GuardedInvocation(methodHandle, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class); 2308 } 2309 2310 /** 2311 * Fall back if a function property is not found. 2312 * @param desc The call site descriptor 2313 * @param request the link request 2314 * @return GuardedInvocation to be invoked at call site. 2315 */ 2316 public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) { 2317 final String name = NashornCallSiteDescriptor.getOperand(desc); 2318 final FindProperty find = findProperty(NO_SUCH_METHOD_NAME, true); 2319 final boolean scopeCall = isScope() && NashornCallSiteDescriptor.isScope(desc); 2320 2321 if (find == null) { 2322 return noSuchProperty(desc, request); 2323 } 2324 2325 final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request); 2326 2327 final Object value = find.getObjectValue(); 2328 if (!(value instanceof ScriptFunction)) { 2329 return createEmptyGetter(desc, explicitInstanceOfCheck, name); 2330 } 2331 2332 final ScriptFunction func = (ScriptFunction)value; 2333 final Object thiz = scopeCall && func.isStrict() ? UNDEFINED : this; 2334 // TODO: It'd be awesome if we could bind "name" without binding "this". 2335 // Since we're binding this we must use an identity guard here. 2336 return new GuardedInvocation( 2337 MH.dropArguments( 2338 MH.constant( 2339 ScriptFunction.class, 2340 func.createBound(thiz, new Object[] { name })), 2341 0, 2342 Object.class), 2343 NashornGuards.combineGuards( 2344 NashornGuards.getIdentityGuard(this), 2345 NashornGuards.getMapGuard(getMap(), true))); 2346 } 2347 2348 /** 2349 * Fall back if a property is not found. 2350 * @param desc the call site descriptor. 2351 * @param request the link request 2352 * @return GuardedInvocation to be invoked at call site. 2353 */ 2354 public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) { 2355 final String name = NashornCallSiteDescriptor.getOperand(desc); 2356 final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); 2357 final boolean scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc); 2358 2359 if (find != null) { 2360 final Object value = find.getObjectValue(); 2361 ScriptFunction func = null; 2362 MethodHandle mh = null; 2363 2364 if (value instanceof ScriptFunction) { 2365 func = (ScriptFunction)value; 2366 mh = getCallMethodHandle(func, desc.getMethodType(), name); 2367 } 2368 2369 if (mh != null) { 2370 assert func != null; 2371 if (scopeAccess && func.isStrict()) { 2372 mh = bindTo(mh, UNDEFINED); 2373 } 2374 2375 return new GuardedInvocation( 2376 mh, 2377 find.isSelf()? 2378 getKnownFunctionPropertyGuardSelf( 2379 getMap(), 2380 find.getGetter(Object.class, INVALID_PROGRAM_POINT, request), 2381 func) 2382 : 2383 //TODO this always does a scriptobject check 2384 getKnownFunctionPropertyGuardProto( 2385 getMap(), 2386 find.getGetter(Object.class, INVALID_PROGRAM_POINT, request), 2387 find.getProtoChainLength(), 2388 func), 2389 getProtoSwitchPoints(NO_SUCH_PROPERTY_NAME, find.getOwner()), 2390 //TODO this doesn't need a ClassCastException as guard always checks script object 2391 null); 2392 } 2393 } 2394 2395 if (scopeAccess) { 2396 throw referenceError("not.defined", name); 2397 } 2398 2399 return createEmptyGetter(desc, explicitInstanceOfCheck(desc, request), name); 2400 } 2401 2402 /** 2403 * Invoke fall back if a property is not found. 2404 * @param key Name of property. 2405 * @param isScope is this a scope access? 2406 * @param programPoint program point 2407 * @return Result from call. 2408 */ 2409 protected Object invokeNoSuchProperty(final Object key, final boolean isScope, final int programPoint) { 2410 final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); 2411 final Object func = (find != null)? find.getObjectValue() : null; 2412 2413 Object ret = UNDEFINED; 2414 if (func instanceof ScriptFunction) { 2415 final ScriptFunction sfunc = (ScriptFunction)func; 2416 final Object self = isScope && sfunc.isStrict()? UNDEFINED : this; 2417 ret = ScriptRuntime.apply(sfunc, self, key); 2418 } else if (isScope) { 2419 throw referenceError("not.defined", key.toString()); 2420 } 2421 2422 if (isValid(programPoint)) { 2423 throw new UnwarrantedOptimismException(ret, programPoint); 2424 } 2425 2426 return ret; 2427 } 2428 2429 2430 /** 2431 * Get __noSuchMethod__ as a function bound to this object and {@code name} if it is defined. 2432 * @param name the method name 2433 * @param isScope is this a scope access? 2434 * @return the bound function, or undefined 2435 */ 2436 private Object getNoSuchMethod(final String name, final boolean isScope, final int programPoint) { 2437 final FindProperty find = findProperty(NO_SUCH_METHOD_NAME, true); 2438 2439 if (find == null) { 2440 return invokeNoSuchProperty(name, isScope, programPoint); 2441 } 2442 2443 final Object value = find.getObjectValue(); 2444 if (!(value instanceof ScriptFunction)) { 2445 if (isScope) { 2446 throw referenceError("not.defined", name); 2447 } 2448 return UNDEFINED; 2449 } 2450 2451 final ScriptFunction func = (ScriptFunction)value; 2452 final Object self = isScope && func.isStrict()? UNDEFINED : this; 2453 return func.createBound(self, new Object[] {name}); 2454 } 2455 2456 private GuardedInvocation createEmptyGetter(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String name) { 2457 if (NashornCallSiteDescriptor.isOptimistic(desc)) { 2458 throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT); 2459 } 2460 2461 return new GuardedInvocation(Lookup.emptyGetter(desc.getMethodType().returnType()), 2462 NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck), getProtoSwitchPoints(name, null), 2463 explicitInstanceOfCheck ? null : ClassCastException.class); 2464 } 2465 2466 private abstract static class ScriptObjectIterator <T extends Object> implements Iterator<T> { 2467 protected T[] values; 2468 protected final ScriptObject object; 2469 private int index; 2470 2471 ScriptObjectIterator(final ScriptObject object) { 2472 this.object = object; 2473 } 2474 2475 protected abstract void init(); 2476 2477 @Override 2478 public boolean hasNext() { 2479 if (values == null) { 2480 init(); 2481 } 2482 return index < values.length; 2483 } 2484 2485 @Override 2486 public T next() { 2487 if (values == null) { 2488 init(); 2489 } 2490 return values[index++]; 2491 } 2492 2493 @Override 2494 public void remove() { 2495 throw new UnsupportedOperationException("remove"); 2496 } 2497 } 2498 2499 private static class KeyIterator extends ScriptObjectIterator<String> { 2500 KeyIterator(final ScriptObject object) { 2501 super(object); 2502 } 2503 2504 @Override 2505 protected void init() { 2506 final Set<String> keys = new LinkedHashSet<>(); 2507 final Set<String> nonEnumerable = new HashSet<>(); 2508 for (ScriptObject self = object; self != null; self = self.getProto()) { 2509 keys.addAll(Arrays.asList(self.getOwnKeys(String.class, false, nonEnumerable))); 2510 } 2511 this.values = keys.toArray(new String[0]); 2512 } 2513 } 2514 2515 private static class ValueIterator extends ScriptObjectIterator<Object> { 2516 ValueIterator(final ScriptObject object) { 2517 super(object); 2518 } 2519 2520 @Override 2521 protected void init() { 2522 final ArrayList<Object> valueList = new ArrayList<>(); 2523 final Set<String> nonEnumerable = new HashSet<>(); 2524 for (ScriptObject self = object; self != null; self = self.getProto()) { 2525 for (final String key : self.getOwnKeys(String.class, false, nonEnumerable)) { 2526 valueList.add(self.get(key)); 2527 } 2528 } 2529 this.values = valueList.toArray(new Object[0]); 2530 } 2531 } 2532 2533 /** 2534 * Add a spill property for the given key. 2535 * @param key Property key. 2536 * @param flags Property flags. 2537 * @return Added property. 2538 */ 2539 private Property addSpillProperty(final Object key, final int flags, final Object value, final boolean hasInitialValue) { 2540 final PropertyMap propertyMap = getMap(); 2541 final int fieldSlot = propertyMap.getFreeFieldSlot(); 2542 final int propertyFlags = flags | (useDualFields() ? Property.DUAL_FIELDS : 0); 2543 2544 Property property; 2545 if (fieldSlot > -1) { 2546 property = hasInitialValue ? 2547 new AccessorProperty(key, propertyFlags, fieldSlot, this, value) : 2548 new AccessorProperty(key, propertyFlags, getClass(), fieldSlot); 2549 property = addOwnProperty(property); 2550 } else { 2551 final int spillSlot = propertyMap.getFreeSpillSlot(); 2552 property = hasInitialValue ? 2553 new SpillProperty(key, propertyFlags, spillSlot, this, value) : 2554 new SpillProperty(key, propertyFlags, spillSlot); 2555 property = addOwnProperty(property); 2556 ensureSpillSize(property.getSlot()); 2557 } 2558 return property; 2559 } 2560 2561 /** 2562 * Add a spill entry for the given key. 2563 * @param key Property key. 2564 * @return Setter method handle. 2565 */ 2566 MethodHandle addSpill(final Class<?> type, final String key) { 2567 return addSpillProperty(key, 0, null, false).getSetter(type, getMap()); 2568 } 2569 2570 /** 2571 * Make sure arguments are paired correctly, with respect to more parameters than declared, 2572 * fewer parameters than declared and other things that JavaScript allows. This might involve 2573 * creating collectors. 2574 * 2575 * @param methodHandle method handle for invoke 2576 * @param callType type of the call 2577 * 2578 * @return method handle with adjusted arguments 2579 */ 2580 protected static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType) { 2581 return pairArguments(methodHandle, callType, null); 2582 } 2583 2584 /** 2585 * Make sure arguments are paired correctly, with respect to more parameters than declared, 2586 * fewer parameters than declared and other things that JavaScript allows. This might involve 2587 * creating collectors. 2588 * 2589 * Make sure arguments are paired correctly. 2590 * @param methodHandle MethodHandle to adjust. 2591 * @param callType MethodType of the call site. 2592 * @param callerVarArg true if the caller is vararg, false otherwise, null if it should be inferred from the 2593 * {@code callType}; basically, if the last parameter type of the call site is an array, it'll be considered a 2594 * variable arity call site. These are ordinarily rare; Nashorn code generator creates variable arity call sites 2595 * when the call has more than {@link LinkerCallSite#ARGLIMIT} parameters. 2596 * 2597 * @return method handle with adjusted arguments 2598 */ 2599 public static MethodHandle pairArguments(final MethodHandle methodHandle, final MethodType callType, final Boolean callerVarArg) { 2600 final MethodType methodType = methodHandle.type(); 2601 if (methodType.equals(callType.changeReturnType(methodType.returnType()))) { 2602 return methodHandle; 2603 } 2604 2605 final int parameterCount = methodType.parameterCount(); 2606 final int callCount = callType.parameterCount(); 2607 2608 final boolean isCalleeVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray(); 2609 final boolean isCallerVarArg = callerVarArg != null ? callerVarArg : callCount > 0 && 2610 callType.parameterType(callCount - 1).isArray(); 2611 2612 if (isCalleeVarArg) { 2613 return isCallerVarArg ? 2614 methodHandle : 2615 MH.asCollector(methodHandle, Object[].class, callCount - parameterCount + 1); 2616 } 2617 2618 if (isCallerVarArg) { 2619 return adaptHandleToVarArgCallSite(methodHandle, callCount); 2620 } 2621 2622 if (callCount < parameterCount) { 2623 final int missingArgs = parameterCount - callCount; 2624 final Object[] fillers = new Object[missingArgs]; 2625 2626 Arrays.fill(fillers, UNDEFINED); 2627 2628 if (isCalleeVarArg) { 2629 fillers[missingArgs - 1] = ScriptRuntime.EMPTY_ARRAY; 2630 } 2631 2632 return MH.insertArguments( 2633 methodHandle, 2634 parameterCount - missingArgs, 2635 fillers); 2636 } 2637 2638 if (callCount > parameterCount) { 2639 final int discardedArgs = callCount - parameterCount; 2640 2641 final Class<?>[] discards = new Class<?>[discardedArgs]; 2642 Arrays.fill(discards, Object.class); 2643 2644 return MH.dropArguments(methodHandle, callCount - discardedArgs, discards); 2645 } 2646 2647 return methodHandle; 2648 } 2649 2650 static MethodHandle adaptHandleToVarArgCallSite(final MethodHandle mh, final int callSiteParamCount) { 2651 final int spreadArgs = mh.type().parameterCount() - callSiteParamCount + 1; 2652 return MH.filterArguments( 2653 MH.asSpreader( 2654 mh, 2655 Object[].class, 2656 spreadArgs), 2657 callSiteParamCount - 1, 2658 MH.insertArguments( 2659 TRUNCATINGFILTER, 2660 0, 2661 spreadArgs) 2662 ); 2663 } 2664 2665 @SuppressWarnings("unused") 2666 private static Object[] truncatingFilter(final int n, final Object[] array) { 2667 final int length = array == null ? 0 : array.length; 2668 if (n == length) { 2669 return array == null ? ScriptRuntime.EMPTY_ARRAY : array; 2670 } 2671 2672 final Object[] newArray = new Object[n]; 2673 2674 if (array != null) { 2675 System.arraycopy(array, 0, newArray, 0, Math.min(n, length)); 2676 } 2677 2678 if (length < n) { 2679 final Object fill = UNDEFINED; 2680 2681 for (int i = length; i < n; i++) { 2682 newArray[i] = fill; 2683 } 2684 } 2685 2686 return newArray; 2687 } 2688 2689 /** 2690 * Numeric length setter for length property 2691 * 2692 * @param newLength new length to set 2693 */ 2694 public final void setLength(final long newLength) { 2695 final ArrayData data = getArray(); 2696 final long arrayLength = data.length(); 2697 if (newLength == arrayLength) { 2698 return; 2699 } 2700 2701 if (newLength > arrayLength) { 2702 setArray(data.ensure(newLength - 1).safeDelete(arrayLength, newLength - 1, false)); 2703 return; 2704 } 2705 2706 if (newLength < arrayLength) { 2707 long actualLength = newLength; 2708 2709 // Check for numeric keys in property map and delete them or adjust length, depending on whether 2710 // they're defined as configurable. See ES5 #15.4.5.2 2711 if (getMap().containsArrayKeys()) { 2712 2713 for (long l = arrayLength - 1; l >= newLength; l--) { 2714 final FindProperty find = findProperty(JSType.toString(l), false); 2715 2716 if (find != null) { 2717 2718 if (find.getProperty().isConfigurable()) { 2719 deleteOwnProperty(find.getProperty()); 2720 } else { 2721 actualLength = l + 1; 2722 break; 2723 } 2724 } 2725 } 2726 } 2727 2728 setArray(data.shrink(actualLength)); 2729 data.setLength(actualLength); 2730 } 2731 } 2732 2733 private int getInt(final int index, final Object key, final int programPoint) { 2734 if (isValidArrayIndex(index)) { 2735 for (ScriptObject object = this; ; ) { 2736 if (object.getMap().containsArrayKeys()) { 2737 final FindProperty find = object.findProperty(key, false, this); 2738 2739 if (find != null) { 2740 return getIntValue(find, programPoint); 2741 } 2742 } 2743 2744 if ((object = object.getProto()) == null) { 2745 break; 2746 } 2747 2748 final ArrayData array = object.getArray(); 2749 2750 if (array.has(index)) { 2751 return isValid(programPoint) ? 2752 array.getIntOptimistic(index, programPoint) : 2753 array.getInt(index); 2754 } 2755 } 2756 } else { 2757 final FindProperty find = findProperty(key, true); 2758 2759 if (find != null) { 2760 return getIntValue(find, programPoint); 2761 } 2762 } 2763 2764 return JSType.toInt32(invokeNoSuchProperty(key, false, programPoint)); 2765 } 2766 2767 @Override 2768 public int getInt(final Object key, final int programPoint) { 2769 final Object primitiveKey = JSType.toPrimitive(key, String.class); 2770 final int index = getArrayIndex(primitiveKey); 2771 final ArrayData array = getArray(); 2772 2773 if (array.has(index)) { 2774 return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index); 2775 } 2776 2777 return getInt(index, JSType.toPropertyKey(primitiveKey), programPoint); 2778 } 2779 2780 @Override 2781 public int getInt(final double key, final int programPoint) { 2782 final int index = getArrayIndex(key); 2783 final ArrayData array = getArray(); 2784 2785 if (array.has(index)) { 2786 return isValid(programPoint) ? array.getIntOptimistic(index, programPoint) : array.getInt(index); 2787 } 2788 2789 return getInt(index, JSType.toString(key), programPoint); 2790 } 2791 2792 @Override 2793 public int getInt(final int key, final int programPoint) { 2794 final int index = getArrayIndex(key); 2795 final ArrayData array = getArray(); 2796 2797 if (array.has(index)) { 2798 return isValid(programPoint) ? array.getIntOptimistic(key, programPoint) : array.getInt(key); 2799 } 2800 2801 return getInt(index, JSType.toString(key), programPoint); 2802 } 2803 2804 private double getDouble(final int index, final Object key, final int programPoint) { 2805 if (isValidArrayIndex(index)) { 2806 for (ScriptObject object = this; ; ) { 2807 if (object.getMap().containsArrayKeys()) { 2808 final FindProperty find = object.findProperty(key, false, this); 2809 if (find != null) { 2810 return getDoubleValue(find, programPoint); 2811 } 2812 } 2813 2814 if ((object = object.getProto()) == null) { 2815 break; 2816 } 2817 2818 final ArrayData array = object.getArray(); 2819 2820 if (array.has(index)) { 2821 return isValid(programPoint) ? 2822 array.getDoubleOptimistic(index, programPoint) : 2823 array.getDouble(index); 2824 } 2825 } 2826 } else { 2827 final FindProperty find = findProperty(key, true); 2828 2829 if (find != null) { 2830 return getDoubleValue(find, programPoint); 2831 } 2832 } 2833 2834 return JSType.toNumber(invokeNoSuchProperty(key, false, INVALID_PROGRAM_POINT)); 2835 } 2836 2837 @Override 2838 public double getDouble(final Object key, final int programPoint) { 2839 final Object primitiveKey = JSType.toPrimitive(key, String.class); 2840 final int index = getArrayIndex(primitiveKey); 2841 final ArrayData array = getArray(); 2842 2843 if (array.has(index)) { 2844 return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index); 2845 } 2846 2847 return getDouble(index, JSType.toPropertyKey(primitiveKey), programPoint); 2848 } 2849 2850 @Override 2851 public double getDouble(final double key, final int programPoint) { 2852 final int index = getArrayIndex(key); 2853 final ArrayData array = getArray(); 2854 2855 if (array.has(index)) { 2856 return isValid(programPoint) ? array.getDoubleOptimistic(index, programPoint) : array.getDouble(index); 2857 } 2858 2859 return getDouble(index, JSType.toString(key), programPoint); 2860 } 2861 2862 @Override 2863 public double getDouble(final int key, final int programPoint) { 2864 final int index = getArrayIndex(key); 2865 final ArrayData array = getArray(); 2866 2867 if (array.has(index)) { 2868 return isValid(programPoint) ? array.getDoubleOptimistic(key, programPoint) : array.getDouble(key); 2869 } 2870 2871 return getDouble(index, JSType.toString(key), programPoint); 2872 } 2873 2874 private Object get(final int index, final Object key) { 2875 if (isValidArrayIndex(index)) { 2876 for (ScriptObject object = this; ; ) { 2877 if (object.getMap().containsArrayKeys()) { 2878 final FindProperty find = object.findProperty(key, false, this); 2879 2880 if (find != null) { 2881 return find.getObjectValue(); 2882 } 2883 } 2884 2885 if ((object = object.getProto()) == null) { 2886 break; 2887 } 2888 2889 final ArrayData array = object.getArray(); 2890 2891 if (array.has(index)) { 2892 return array.getObject(index); 2893 } 2894 } 2895 } else { 2896 final FindProperty find = findProperty(key, true); 2897 2898 if (find != null) { 2899 return find.getObjectValue(); 2900 } 2901 } 2902 2903 return invokeNoSuchProperty(key, false, INVALID_PROGRAM_POINT); 2904 } 2905 2906 @Override 2907 public Object get(final Object key) { 2908 final Object primitiveKey = JSType.toPrimitive(key, String.class); 2909 final int index = getArrayIndex(primitiveKey); 2910 final ArrayData array = getArray(); 2911 2912 if (array.has(index)) { 2913 return array.getObject(index); 2914 } 2915 2916 return get(index, JSType.toPropertyKey(primitiveKey)); 2917 } 2918 2919 @Override 2920 public Object get(final double key) { 2921 final int index = getArrayIndex(key); 2922 final ArrayData array = getArray(); 2923 2924 if (array.has(index)) { 2925 return array.getObject(index); 2926 } 2927 2928 return get(index, JSType.toString(key)); 2929 } 2930 2931 @Override 2932 public Object get(final int key) { 2933 final int index = getArrayIndex(key); 2934 final ArrayData array = getArray(); 2935 2936 if (array.has(index)) { 2937 return array.getObject(index); 2938 } 2939 2940 return get(index, JSType.toString(key)); 2941 } 2942 2943 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final int value, final int callSiteFlags) { 2944 if (getMap().containsArrayKeys()) { 2945 final String key = JSType.toString(longIndex); 2946 final FindProperty find = findProperty(key, true); 2947 if (find != null) { 2948 setObject(find, callSiteFlags, key, value); 2949 return true; 2950 } 2951 } 2952 return false; 2953 } 2954 2955 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final long value, final int callSiteFlags) { 2956 if (getMap().containsArrayKeys()) { 2957 final String key = JSType.toString(longIndex); 2958 final FindProperty find = findProperty(key, true); 2959 if (find != null) { 2960 setObject(find, callSiteFlags, key, value); 2961 return true; 2962 } 2963 } 2964 return false; 2965 } 2966 2967 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final double value, final int callSiteFlags) { 2968 if (getMap().containsArrayKeys()) { 2969 final String key = JSType.toString(longIndex); 2970 final FindProperty find = findProperty(key, true); 2971 if (find != null) { 2972 setObject(find, callSiteFlags, key, value); 2973 return true; 2974 } 2975 } 2976 return false; 2977 } 2978 2979 private boolean doesNotHaveCheckArrayKeys(final long longIndex, final Object value, final int callSiteFlags) { 2980 if (getMap().containsArrayKeys()) { 2981 final String key = JSType.toString(longIndex); 2982 final FindProperty find = findProperty(key, true); 2983 if (find != null) { 2984 setObject(find, callSiteFlags, key, value); 2985 return true; 2986 } 2987 } 2988 return false; 2989 } 2990 2991 //value agnostic 2992 private boolean doesNotHaveEnsureLength(final long longIndex, final long oldLength, final int callSiteFlags) { 2993 if (longIndex >= oldLength) { 2994 if (!isExtensible()) { 2995 if (isStrictFlag(callSiteFlags)) { 2996 throw typeError("object.non.extensible", JSType.toString(longIndex), ScriptRuntime.safeToString(this)); 2997 } 2998 return true; 2999 } 3000 setArray(getArray().ensure(longIndex)); 3001 } 3002 return false; 3003 } 3004 3005 private void doesNotHave(final int index, final int value, final int callSiteFlags) { 3006 final long oldLength = getArray().length(); 3007 final long longIndex = ArrayIndex.toLongIndex(index); 3008 if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) { 3009 final boolean strict = isStrictFlag(callSiteFlags); 3010 setArray(getArray().set(index, value, strict).safeDelete(oldLength, longIndex - 1, strict)); 3011 } 3012 } 3013 3014 private void doesNotHave(final int index, final double value, final int callSiteFlags) { 3015 final long oldLength = getArray().length(); 3016 final long longIndex = ArrayIndex.toLongIndex(index); 3017 if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) { 3018 final boolean strict = isStrictFlag(callSiteFlags); 3019 setArray(getArray().set(index, value, strict).safeDelete(oldLength, longIndex - 1, strict)); 3020 } 3021 } 3022 3023 private void doesNotHave(final int index, final Object value, final int callSiteFlags) { 3024 final long oldLength = getArray().length(); 3025 final long longIndex = ArrayIndex.toLongIndex(index); 3026 if (!doesNotHaveCheckArrayKeys(longIndex, value, callSiteFlags) && !doesNotHaveEnsureLength(longIndex, oldLength, callSiteFlags)) { 3027 final boolean strict = isStrictFlag(callSiteFlags); 3028 setArray(getArray().set(index, value, strict).safeDelete(oldLength, longIndex - 1, strict)); 3029 } 3030 } 3031 3032 /** 3033 * This is the most generic of all Object setters. Most of the others use this in some form. 3034 * TODO: should be further specialized 3035 * 3036 * @param find found property 3037 * @param callSiteFlags callsite flags 3038 * @param key property key 3039 * @param value property value 3040 */ 3041 public final void setObject(final FindProperty find, final int callSiteFlags, final Object key, final Object value) { 3042 FindProperty f = find; 3043 3044 invalidateGlobalConstant(key); 3045 3046 if (f != null && f.isInherited() && !f.getProperty().isAccessorProperty()) { 3047 final boolean isScope = isScopeFlag(callSiteFlags); 3048 // If the start object of the find is not this object it means the property was found inside a 3049 // 'with' statement expression (see WithObject.findProperty()). In this case we forward the 'set' 3050 // to the 'with' object. 3051 // Note that although a 'set' operation involving a with statement follows scope rules outside 3052 // the 'with' expression (the 'set' operation is performed on the owning prototype if it exists), 3053 // it follows non-scope rules inside the 'with' expression (set is performed on the top level object). 3054 // This is why we clear the callsite flags and FindProperty in the forward call to the 'with' object. 3055 if (isScope && f.getSelf() != this) { 3056 f.getSelf().setObject(null, 0, key, value); 3057 return; 3058 } 3059 // Setting a property should not modify the property in prototype unless this is a scope callsite 3060 // and the owner is a scope object as well (with the exception of 'with' statement handled above). 3061 if (!isScope || !f.getOwner().isScope()) { 3062 f = null; 3063 } 3064 } 3065 3066 if (f != null) { 3067 if (!f.getProperty().isWritable() || !f.getProperty().hasNativeSetter()) { 3068 if (isScopeFlag(callSiteFlags) && f.getProperty().isLexicalBinding()) { 3069 throw typeError("assign.constant", key.toString()); // Overwriting ES6 const should throw also in non-strict mode. 3070 } 3071 if (isStrictFlag(callSiteFlags)) { 3072 throw typeError( 3073 f.getProperty().isAccessorProperty() ? "property.has.no.setter" : "property.not.writable", 3074 key.toString(), ScriptRuntime.safeToString(this)); 3075 } 3076 return; 3077 } 3078 3079 f.setValue(value, isStrictFlag(callSiteFlags)); 3080 3081 } else if (!isExtensible()) { 3082 if (isStrictFlag(callSiteFlags)) { 3083 throw typeError("object.non.extensible", key.toString(), ScriptRuntime.safeToString(this)); 3084 } 3085 } else { 3086 ScriptObject sobj = this; 3087 // undefined scope properties are set in the global object. 3088 if (isScope()) { 3089 while (sobj != null && !(sobj instanceof Global)) { 3090 sobj = sobj.getProto(); 3091 } 3092 assert sobj != null : "no parent global object in scope"; 3093 } 3094 //this will unbox any Number object to its primitive type in case the 3095 //property supports primitive types, so it doesn't matter that it comes 3096 //in as an Object. 3097 sobj.addSpillProperty(key, 0, value, true); 3098 } 3099 } 3100 3101 @Override 3102 public void set(final Object key, final int value, final int callSiteFlags) { 3103 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3104 final int index = getArrayIndex(primitiveKey); 3105 3106 if (isValidArrayIndex(index)) { 3107 final ArrayData data = getArray(); 3108 if (data.has(index)) { 3109 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3110 } else { 3111 doesNotHave(index, value, callSiteFlags); 3112 } 3113 3114 return; 3115 } 3116 3117 final Object propName = JSType.toPropertyKey(primitiveKey); 3118 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3119 } 3120 3121 @Override 3122 public void set(final Object key, final double value, final int callSiteFlags) { 3123 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3124 final int index = getArrayIndex(primitiveKey); 3125 3126 if (isValidArrayIndex(index)) { 3127 final ArrayData data = getArray(); 3128 if (data.has(index)) { 3129 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3130 } else { 3131 doesNotHave(index, value, callSiteFlags); 3132 } 3133 3134 return; 3135 } 3136 3137 final Object propName = JSType.toPropertyKey(primitiveKey); 3138 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3139 } 3140 3141 @Override 3142 public void set(final Object key, final Object value, final int callSiteFlags) { 3143 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3144 final int index = getArrayIndex(primitiveKey); 3145 3146 if (isValidArrayIndex(index)) { 3147 final ArrayData data = getArray(); 3148 if (data.has(index)) { 3149 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3150 } else { 3151 doesNotHave(index, value, callSiteFlags); 3152 } 3153 3154 return; 3155 } 3156 3157 final Object propName = JSType.toPropertyKey(primitiveKey); 3158 setObject(findProperty(propName, true), callSiteFlags, propName, value); 3159 } 3160 3161 @Override 3162 public void set(final double key, final int value, final int callSiteFlags) { 3163 final int index = getArrayIndex(key); 3164 3165 if (isValidArrayIndex(index)) { 3166 final ArrayData data = getArray(); 3167 if (data.has(index)) { 3168 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3169 } else { 3170 doesNotHave(index, value, callSiteFlags); 3171 } 3172 3173 return; 3174 } 3175 3176 final String propName = JSType.toString(key); 3177 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3178 } 3179 3180 @Override 3181 public void set(final double key, final double value, final int callSiteFlags) { 3182 final int index = getArrayIndex(key); 3183 3184 if (isValidArrayIndex(index)) { 3185 final ArrayData data = getArray(); 3186 if (data.has(index)) { 3187 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3188 } else { 3189 doesNotHave(index, value, callSiteFlags); 3190 } 3191 3192 return; 3193 } 3194 3195 final String propName = JSType.toString(key); 3196 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3197 } 3198 3199 @Override 3200 public void set(final double key, final Object value, final int callSiteFlags) { 3201 final int index = getArrayIndex(key); 3202 3203 if (isValidArrayIndex(index)) { 3204 final ArrayData data = getArray(); 3205 if (data.has(index)) { 3206 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3207 } else { 3208 doesNotHave(index, value, callSiteFlags); 3209 } 3210 3211 return; 3212 } 3213 3214 final String propName = JSType.toString(key); 3215 setObject(findProperty(propName, true), callSiteFlags, propName, value); 3216 } 3217 3218 @Override 3219 public void set(final int key, final int value, final int callSiteFlags) { 3220 final int index = getArrayIndex(key); 3221 if (isValidArrayIndex(index)) { 3222 if (getArray().has(index)) { 3223 final ArrayData data = getArray(); 3224 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3225 } else { 3226 doesNotHave(index, value, callSiteFlags); 3227 } 3228 return; 3229 } 3230 3231 final String propName = JSType.toString(key); 3232 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3233 } 3234 3235 @Override 3236 public void set(final int key, final double value, final int callSiteFlags) { 3237 final int index = getArrayIndex(key); 3238 3239 if (isValidArrayIndex(index)) { 3240 final ArrayData data = getArray(); 3241 if (data.has(index)) { 3242 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3243 } else { 3244 doesNotHave(index, value, callSiteFlags); 3245 } 3246 3247 return; 3248 } 3249 3250 final String propName = JSType.toString(key); 3251 setObject(findProperty(propName, true), callSiteFlags, propName, JSType.toObject(value)); 3252 } 3253 3254 @Override 3255 public void set(final int key, final Object value, final int callSiteFlags) { 3256 final int index = getArrayIndex(key); 3257 3258 if (isValidArrayIndex(index)) { 3259 final ArrayData data = getArray(); 3260 if (data.has(index)) { 3261 setArray(data.set(index, value, isStrictFlag(callSiteFlags))); 3262 } else { 3263 doesNotHave(index, value, callSiteFlags); 3264 } 3265 3266 return; 3267 } 3268 3269 final String propName = JSType.toString(key); 3270 setObject(findProperty(propName, true), callSiteFlags, propName, value); 3271 } 3272 3273 @Override 3274 public boolean has(final Object key) { 3275 final Object primitiveKey = JSType.toPrimitive(key); 3276 final int index = getArrayIndex(primitiveKey); 3277 return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toPropertyKey(primitiveKey), true); 3278 } 3279 3280 @Override 3281 public boolean has(final double key) { 3282 final int index = getArrayIndex(key); 3283 return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true); 3284 } 3285 3286 @Override 3287 public boolean has(final int key) { 3288 final int index = getArrayIndex(key); 3289 return isValidArrayIndex(index) ? hasArrayProperty(index) : hasProperty(JSType.toString(key), true); 3290 } 3291 3292 private boolean hasArrayProperty(final int index) { 3293 boolean hasArrayKeys = false; 3294 3295 for (ScriptObject self = this; self != null; self = self.getProto()) { 3296 if (self.getArray().has(index)) { 3297 return true; 3298 } 3299 hasArrayKeys = hasArrayKeys || self.getMap().containsArrayKeys(); 3300 } 3301 3302 return hasArrayKeys && hasProperty(ArrayIndex.toKey(index), true); 3303 } 3304 3305 @Override 3306 public boolean hasOwnProperty(final Object key) { 3307 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3308 final int index = getArrayIndex(primitiveKey); 3309 return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toPropertyKey(primitiveKey), false); 3310 } 3311 3312 @Override 3313 public boolean hasOwnProperty(final int key) { 3314 final int index = getArrayIndex(key); 3315 return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false); 3316 } 3317 3318 @Override 3319 public boolean hasOwnProperty(final double key) { 3320 final int index = getArrayIndex(key); 3321 return isValidArrayIndex(index) ? hasOwnArrayProperty(index) : hasProperty(JSType.toString(key), false); 3322 } 3323 3324 private boolean hasOwnArrayProperty(final int index) { 3325 return getArray().has(index) || getMap().containsArrayKeys() && hasProperty(ArrayIndex.toKey(index), false); 3326 } 3327 3328 @Override 3329 public boolean delete(final int key, final boolean strict) { 3330 final int index = getArrayIndex(key); 3331 final ArrayData array = getArray(); 3332 3333 if (array.has(index)) { 3334 if (array.canDelete(index, strict)) { 3335 setArray(array.delete(index)); 3336 return true; 3337 } 3338 return false; 3339 } 3340 return deleteObject(JSType.toObject(key), strict); 3341 } 3342 3343 @Override 3344 public boolean delete(final double key, final boolean strict) { 3345 final int index = getArrayIndex(key); 3346 final ArrayData array = getArray(); 3347 3348 if (array.has(index)) { 3349 if (array.canDelete(index, strict)) { 3350 setArray(array.delete(index)); 3351 return true; 3352 } 3353 return false; 3354 } 3355 3356 return deleteObject(JSType.toObject(key), strict); 3357 } 3358 3359 @Override 3360 public boolean delete(final Object key, final boolean strict) { 3361 final Object primitiveKey = JSType.toPrimitive(key, String.class); 3362 final int index = getArrayIndex(primitiveKey); 3363 final ArrayData array = getArray(); 3364 3365 if (array.has(index)) { 3366 if (array.canDelete(index, strict)) { 3367 setArray(array.delete(index)); 3368 return true; 3369 } 3370 return false; 3371 } 3372 3373 return deleteObject(primitiveKey, strict); 3374 } 3375 3376 private boolean deleteObject(final Object key, final boolean strict) { 3377 final Object propName = JSType.toPropertyKey(key); 3378 final FindProperty find = findProperty(propName, false); 3379 3380 if (find == null) { 3381 return true; 3382 } 3383 3384 if (!find.getProperty().isConfigurable()) { 3385 if (strict) { 3386 throw typeError("cant.delete.property", propName.toString(), ScriptRuntime.safeToString(this)); 3387 } 3388 return false; 3389 } 3390 3391 final Property prop = find.getProperty(); 3392 deleteOwnProperty(prop); 3393 3394 return true; 3395 } 3396 3397 /** 3398 * Return a shallow copy of this ScriptObject. 3399 * @return a shallow copy. 3400 */ 3401 public final ScriptObject copy() { 3402 try { 3403 return clone(); 3404 } catch (final CloneNotSupportedException e) { 3405 throw new RuntimeException(e); 3406 } 3407 } 3408 3409 @Override 3410 protected ScriptObject clone() throws CloneNotSupportedException { 3411 final ScriptObject clone = (ScriptObject) super.clone(); 3412 if (objectSpill != null) { 3413 clone.objectSpill = objectSpill.clone(); 3414 if (primitiveSpill != null) { 3415 clone.primitiveSpill = primitiveSpill.clone(); 3416 } 3417 } 3418 clone.arrayData = arrayData.copy(); 3419 return clone; 3420 } 3421 3422 /** 3423 * Make a new UserAccessorProperty property. getter and setter functions are stored in 3424 * this ScriptObject and slot values are used in property object. 3425 * 3426 * @param key the property name 3427 * @param propertyFlags attribute flags of the property 3428 * @param getter getter function for the property 3429 * @param setter setter function for the property 3430 * @return the newly created UserAccessorProperty 3431 */ 3432 protected final UserAccessorProperty newUserAccessors(final Object key, final int propertyFlags, final ScriptFunction getter, final ScriptFunction setter) { 3433 final UserAccessorProperty uc = getMap().newUserAccessors(key, propertyFlags); 3434 //property.getSetter(Object.class, getMap()); 3435 uc.setAccessors(this, getMap(), new UserAccessorProperty.Accessors(getter, setter)); 3436 return uc; 3437 } 3438 3439 /** 3440 * Returns {@code true} if properties for this object should use dual field mode, {@code false} otherwise. 3441 * @return {@code true} if dual fields should be used. 3442 */ 3443 protected boolean useDualFields() { 3444 return !StructureLoader.isSingleFieldStructure(getClass().getName()); 3445 } 3446 3447 Object ensureSpillSize(final int slot) { 3448 final int oldLength = objectSpill == null ? 0 : objectSpill.length; 3449 if (slot < oldLength) { 3450 return this; 3451 } 3452 final int newLength = alignUp(slot + 1, SPILL_RATE); 3453 final Object[] newObjectSpill = new Object[newLength]; 3454 final long[] newPrimitiveSpill = useDualFields() ? new long[newLength] : null; 3455 3456 if (objectSpill != null) { 3457 System.arraycopy(objectSpill, 0, newObjectSpill, 0, oldLength); 3458 if (primitiveSpill != null && newPrimitiveSpill != null) { 3459 System.arraycopy(primitiveSpill, 0, newPrimitiveSpill, 0, oldLength); 3460 } 3461 } 3462 3463 this.primitiveSpill = newPrimitiveSpill; 3464 this.objectSpill = newObjectSpill; 3465 3466 return this; 3467 } 3468 3469 private static MethodHandle findOwnMH_V(final Class<? extends ScriptObject> clazz, final String name, final Class<?> rtype, final Class<?>... types) { 3470 // TODO: figure out how can it work for NativeArray$Prototype etc. 3471 return MH.findVirtual(MethodHandles.lookup(), ScriptObject.class, name, MH.type(rtype, types)); 3472 } 3473 3474 private static MethodHandle findOwnMH_V(final String name, final Class<?> rtype, final Class<?>... types) { 3475 return findOwnMH_V(ScriptObject.class, name, rtype, types); 3476 } 3477 3478 private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { 3479 return MH.findStatic(MethodHandles.lookup(), ScriptObject.class, name, MH.type(rtype, types)); 3480 } 3481 3482 private static MethodHandle getKnownFunctionPropertyGuardSelf(final PropertyMap map, final MethodHandle getter, final ScriptFunction func) { 3483 return MH.insertArguments(KNOWNFUNCPROPGUARDSELF, 1, map, getter, func); 3484 } 3485 3486 @SuppressWarnings("unused") 3487 private static boolean knownFunctionPropertyGuardSelf(final Object self, final PropertyMap map, final MethodHandle getter, final ScriptFunction func) { 3488 if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) { 3489 try { 3490 return getter.invokeExact(self) == func; 3491 } catch (final RuntimeException | Error e) { 3492 throw e; 3493 } catch (final Throwable t) { 3494 throw new RuntimeException(t); 3495 } 3496 } 3497 3498 return false; 3499 } 3500 3501 private static MethodHandle getKnownFunctionPropertyGuardProto(final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) { 3502 return MH.insertArguments(KNOWNFUNCPROPGUARDPROTO, 1, map, getter, depth, func); 3503 } 3504 3505 private static ScriptObject getProto(final ScriptObject self, final int depth) { 3506 ScriptObject proto = self; 3507 for (int d = 0; d < depth; d++) { 3508 proto = proto.getProto(); 3509 if (proto == null) { 3510 return null; 3511 } 3512 } 3513 3514 return proto; 3515 } 3516 3517 @SuppressWarnings("unused") 3518 private static boolean knownFunctionPropertyGuardProto(final Object self, final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) { 3519 if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) { 3520 final ScriptObject proto = getProto((ScriptObject)self, depth); 3521 if (proto == null) { 3522 return false; 3523 } 3524 try { 3525 return getter.invokeExact((Object)proto) == func; 3526 } catch (final RuntimeException | Error e) { 3527 throw e; 3528 } catch (final Throwable t) { 3529 throw new RuntimeException(t); 3530 } 3531 } 3532 3533 return false; 3534 } 3535 3536 /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created */ 3537 private static LongAdder count; 3538 3539 static { 3540 if (Context.DEBUG) { 3541 count = new LongAdder(); 3542 } 3543 } 3544 /** 3545 * Get number of {@code ScriptObject} instances created. If not running in debug 3546 * mode this is always 0 3547 * 3548 * @return number of ScriptObjects created 3549 */ 3550 public static long getCount() { 3551 return count.longValue(); 3552 } 3553} 3554