- All Implemented Interfaces:
Serializable,ConstProperties<Scriptable>,DebuggableObject,PropHolder<Scriptable>,Scriptable,SymbolScriptable
- Direct Known Subclasses:
BaseFunction,Environment,ES6Generator,ES6Iterator,IdScriptableObject,NativeArray,NativeArrayBuffer,NativeArrayBufferView,NativeCallSite,NativeConsole,NativeContinuation,NativeFinalizationRegistry,NativeGenerator,NativeIterator,NativeJavaPackage,NativeJSON,NativeMap,NativeObject,NativePromise,NativeRegExp,NativeSet,NativeSymbol,NativeWeakMap,NativeWeakRef,NativeWeakSet,ScopeWrapper,TopLevel.GlobalThis,XMLObject
Various properties and methods of JavaScript objects can be conveniently defined using methods of ScriptableObject.
Classes extending ScriptableObject must define the getClassName method.
- Author:
- Norris Boyd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final classThis comparator sorts property fields in spec-compliant order.static interfaceThis is a single method interface suitable to be implemented as a lambda.static interfaceThis is a single method interface suitable to be implemented as a lambda. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intProperty attribute indicating property is not enumerated.static final intThe empty property attribute.static final intstatic final intProperty attribute indicating property cannot be deleted.static final intProperty attribute indicating assignment to this property is ignored.static final intstatic final intProperty attribute indicating that this is a const property that has not been assigned yet.Fields inherited from class org.htmlunit.corejs.javascript.SlotMapOwner
isSealedFields inherited from interface org.htmlunit.corejs.javascript.Scriptable
NOT_FOUND -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static intapplyDescriptorToAttributeBitset(int attributes, Object enumerable, Object writable, Object configurable) booleanEmulate the SpiderMonkey (and Firefox) feature of allowing custom objects to avoid detection by normal "object detection" code patterns.protected static ScriptableObject.DescriptorInfobuildDataDescriptor(Object value, int attributes) static ObjectcallMethod(Context cx, Scriptable obj, String methodName, Object[] args) Call a method of an object.static ObjectcallMethod(Scriptable obj, String methodName, Object[] args) Call a method of an object.protected static voidcheckNotSealed(ScriptableObject obj, Object key, int index) protected final voidcheckPropertyChangeForSlot(Object id, Slot<?> current, ScriptableObject desc) protected final voidcheckPropertyChangeForSlot(Object id, Slot<?> current, ScriptableObject.DescriptorInfo info) protected static voidprotected static voidprotected static <T extends PropHolder<T>>
Slot<T>checkSlotRemoval(Object key, int index, Slot<T> slot, CompoundOperationMap<T> compoundOp, SlotMapOwner<T> owner) voiddefineBuiltinProperty(VarScope scope, String name, int length, SerializableCallable target) voiddefineBuiltinProperty(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) static <T extends ScriptableObject>
voiddefineBuiltInProperty(T owner, int attributes, BuiltInSlot.Descriptor<T> descriptor) static <T extends Scriptable>
voiddefineClass(VarScope scope, Class<T> clazz) Defines JavaScript objects from a Java class that implements Scriptable.static <T extends Scriptable>
voiddefineClass(VarScope scope, Class<T> clazz, boolean sealed) Defines JavaScript objects from a Java class, optionally allowing sealing.static <T extends Scriptable>
StringdefineClass(VarScope scope, Class<T> clazz, boolean sealed, boolean mapInheritance) Defines JavaScript objects from a Java class, optionally allowing sealing and mapping of Java inheritance to JavaScript prototype-based inheritance.voiddefineConst(Context cx, String name, Scriptable start) Reserves a definition spot for a const.static <T extends PropHolder<T>>
voiddefineConstProperty(Context cx, T destination, String propertyName) Utility method to add properties to arbitrary Scriptable object.voiddefineFunctionProperties(VarScope scope, String[] names, Class<?> clazz, int attributes) Search for names in a class, adding the resulting methods as properties.voiddefineOwnProperties(Context cx, ScriptableObject props) Defines one or more properties on this object.booleandefineOwnProperty(Context cx, Object id, ScriptableObject desc) Defines a property on an object.booleandefineOwnProperty(Context cx, Object id, ScriptableObject.DescriptorInfo desc) protected booleandefineOwnProperty(Context cx, Object id, ScriptableObject.DescriptorInfo desc, boolean checkValid) Defines a property on an object.voidDefine a property on this object that is implemented using lambda functions.voiddefineProperty(Context cx, VarScope scope, String name, ScriptableObject.LambdaGetterFunction getter, int attributes) voiddefineProperty(Context cx, VarScope scope, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) Define a property on this object that is implemented using lambda functions accepting Scriptable `this` object as first parameter.voiddefineProperty(Context cx, VarScope scope, Symbol key, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) voiddefineProperty(VarScope scope, String name, int length, SerializableCallable target) voiddefineProperty(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) Utility method to add lambda properties to arbitrary Scriptable object.voiddefineProperty(VarScope scope, String propertyName, Class<?> clazz, int attributes) Define a JavaScript property with getter and setter side effects.voiddefineProperty(VarScope scope, String propertyName, Object delegateTo, Method getter, Method setter, int attributes) Define a JavaScript property.static <T extends PropHolder<T>>
voiddefineProperty(T destination, String propertyName, Object value, int attributes) Utility method to add properties to arbitrary Scriptable object.static <T extends PropHolder<T>>
voiddefineProperty(T destination, SymbolKey propertyName, Object value, int attributes) voiddelete(int index) Removes the indexed property from the object.voidRemoves a named property from the object.voidRemoves an object like the others, but using a Symbol as the key.static booleandeleteProperty(Scriptable obj, int index) Removes the property from an object or its prototype chain.static booleandeleteProperty(Scriptable obj, String name) Removes the property from an object or its prototype chain.static booleandeleteProperty(Scriptable obj, Symbol key) A version of deleteProperty for properties with Symbol keys.protected static ScriptableensureScriptable(Object arg) protected static ScriptableObjectprotected static ScriptableObjectprotected static SymbolScriptableprotected static <T> TensureType(Object obj, Class<T> clazz, String functionName) protected ObjectequivalentValues(Object value) Custom==operator.get(int index, Scriptable start) Returns the value of the indexed property or NOT_FOUND.Object[]Returns an array of ids of all non-Symbol properties of the object.Get the parent scope of the object.static ScriptablegetArrayPrototype(VarScope scope) abstract StringReturn the name of the class.static ScriptablegetClassPrototype(VarScope scope, String className) Get the prototype for the named class.static ScriptablegetClassPrototype(VarScope scope, Symbol className) getDefaultValue(Class<?> typeHint) Implements the [[DefaultValue]] internal method.static ObjectgetDefaultValue(Scriptable object, Class<?> typeHint) Return the array that was previously set by the call to "setExternalArrayData".This is a function used by setExternalArrayData to dynamically get the "length" property value.static ScriptablegetFunctionPrototype(VarScope scope) Get the Function.prototype property.static ScriptablegetGetterOrSetter(String name, int index, boolean isSetter) Deprecated.since 1.7.14.getGetterOrSetter(String name, int index, Scriptable scope, boolean isSetter) Get the getter or setter for a given property.static ScriptablegetObjectPrototype(VarScope scope) Get the Object.prototype property.protected ScriptableObject.DescriptorInfogetOwnPropertyDescriptor(Context cx, Object id) Returns the parent (enclosing) scope of the object.static ObjectgetProperty(Scriptable obj, int index) Gets an indexed property from an object or any object in its prototype chain.static ObjectgetProperty(Scriptable obj, String name) Gets a named property from an object or any object in its prototype chain.static ObjectgetProperty(Scriptable obj, Symbol key) This is a version of getProperty that works with Symbols.static ObjectgetProperty(VarScope obj, String name) static ObjectgetProperty(VarScope obj, Symbol name) static Object[]getPropertyIds(Scriptable obj) Returns an array of all ids from an object and its prototypes.Returns the prototype of the object.static ObjectgetSuperProperty(Scriptable superObj, Scriptable thisObj, int index) Gets an indexed property from super, walking the super's prototype chain, but passing the correct "this" to getter slots.static ObjectgetSuperProperty(Scriptable superObj, Scriptable thisObj, String name) Gets a named property from super, walking the super's prototype chain, but passing the correct "this" to getter slots.static ObjectgetSuperProperty(Scriptable superObj, Scriptable thisObj, Symbol key) This is a version of getSuperProperty that works with Symbols.getThis()static TopLevelgetTopLevelScope(VarScope obj) Get the global scope.static ObjectgetTopScopeValue(VarScope scope, Object key) Get arbitrary application-specific value associated with the top scope of the given scope.static <T> TgetTypedProperty(Scriptable s, int index, Class<T> type) Gets an indexed property from an object or any object in its prototype chain and coerces it to the requested Java type.static <T> TgetTypedProperty(Scriptable s, String name, Class<T> type) Gets a named property from an object or any object in its prototype chain and coerces it to the requested Java type.Gets the value that will be returned by calling the typeof operator on this object.booleanhas(int index, Scriptable start) Returns true if the property index is defined.booleanhasInstance(Scriptable instance) Implements the instanceof operator.static booleanhasProperty(Scriptable obj, int index) Returns whether an indexed property is defined in an object or any object in its prototype chain.static booleanhasProperty(Scriptable obj, String name) Returns whether a named property is defined in an object or any object in its prototype chain.static booleanhasProperty(Scriptable obj, Symbol key) A version of hasProperty for properties with Symbol keys.static booleanhasProperty(VarScope obj, String name) protected static booleanImplements IsAccessorDescriptor as described in ES5 8.10.1protected static booleanbooleanReturns true if the named property is defined as a const on this object.protected static booleanImplements IsDataDescriptor as described in ES5 8.10.2booleanisEmpty()booleanprotected static booleanprotected static booleanImplements IsGenericDescriptor as described in ES5 8.10.3protected static booleanprotected booleanisGetterOrSetter(String name, int index, boolean setter) Returns whether a property is a getter or a setterprotected booleanisGetterOrSetter(CompoundOperationMap map, String name, int index, boolean setter) protected static booleanbooleanvoidput(int index, Scriptable start, Object value) Sets the value of the indexed property, creating it if need be.voidput(String name, Scriptable start, Object value) Sets the value of the named property, creating it if need be.voidput(Symbol key, Scriptable start, Object value) Implementation of put required by SymbolScriptable objects.voidputConst(Context cx, String name, Scriptable start, Object value) Sets the value of the named const property, creating it if need be.static voidputConstProperty(Context cx, Scriptable obj, String name, Object value) Puts a named property in an object or in an object in its prototype chain.static voidputConstProperty(Context cx, VarScope obj, String name, Object value) protected booleanputOwnProperty(int index, Scriptable start, Object value, boolean isThrow) Set the value of the named property, and return true if the property is actually defined and can be set.protected booleanputOwnProperty(String name, Scriptable start, Object value, boolean isThrow) Set the value of the named property, and return true if the property is actually defined and can be set.protected booleanputOwnProperty(Symbol key, Scriptable start, Object value, boolean isThrow) Set the value of the named property, and return true if the property is actually defined and can be set.static voidputProperty(Scriptable obj, int index, Object value) Puts an indexed property in an object or in an object in its prototype chain.static voidputProperty(Scriptable obj, String name, Object value) Puts a named property in an object or in an object in its prototype chain.static voidputProperty(Scriptable obj, Symbol key, Object value) This is a version of putProperty for Symbol keys.static voidputProperty(VarScope obj, String name, Object value) static voidputSuperProperty(Scriptable superObj, Scriptable thisObj, int index, Object value) Variant of putProperty to handle super[index] = value where index is integerstatic voidputSuperProperty(Scriptable superObj, Scriptable thisObj, String name, Object value) Variant of putProperty to handle super.name = valuestatic voidputSuperProperty(Scriptable superObj, Scriptable thisObj, Symbol key, Object value) Variant of putProperty to handle super[key] = value where key is a symbolprotected final Slot<Scriptable>static voidredefineProperty(Context cx, Scriptable obj, String name, boolean isConst) If hasProperty(obj, name) would return true, then if the property that was found is compatible with the new property, this method just returns.static voidredefineProperty(Context cx, VarScope obj, String name, boolean isConst) protected booleanImplements SameValue as described in ES5 9.12, additionally checking if new value is defined.voidSeal this object.protected voidsetCommonDescriptorProperties(int attributes, boolean defineWritable) voidsetExternalArrayData(VarScope scope, ExternalArrayData array) Attach the specified object to this object, and delegate all indexed property lookups to it.voidsetGetterOrSetter(Object name, int index, Callable getterOrSetter, boolean isSetter) Implement the legacy "__defineGetter__" and "__defineSetter__" methods.voidSets the parent (enclosing) scope of the object.voidSets the prototype of the object.intsize()Methods inherited from class org.htmlunit.corejs.javascript.SlotMapOwner
associateValue, checkNotSealed, copyAssociatedValue, createSlotMap, createSlotMap, defineProperty, defineProperty, get, get, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getIds, has, has, isSealed, readMaps, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, writeMapsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.htmlunit.corejs.javascript.Scriptable
get, get, getIds, has, hasMethods inherited from interface org.htmlunit.corejs.javascript.SymbolScriptable
get, has
-
Field Details
-
EMPTY
public static final int EMPTYThe empty property attribute.Used by getAttributes() and setAttributes().
-
READONLY
public static final int READONLYProperty attribute indicating assignment to this property is ignored. -
DONTENUM
public static final int DONTENUMProperty attribute indicating property is not enumerated.Only enumerated properties will be returned by getIds().
-
PERMANENT
public static final int PERMANENTProperty attribute indicating property cannot be deleted. -
UNINITIALIZED_CONST
public static final int UNINITIALIZED_CONSTProperty attribute indicating that this is a const property that has not been assigned yet. The first 'const' assignment to the property will clear this bit.- See Also:
-
STRICTLY_READONLY
public static final int STRICTLY_READONLY- See Also:
-
LEGACY_CONST
public static final int LEGACY_CONST- See Also:
-
CONST
public static final int CONST- See Also:
-
-
Constructor Details
-
ScriptableObject
public ScriptableObject() -
ScriptableObject
-
-
Method Details
-
buildDataDescriptor
-
setCommonDescriptorProperties
protected void setCommonDescriptorProperties(int attributes, boolean defineWritable) -
getTypeOf
Gets the value that will be returned by calling the typeof operator on this object.- Returns:
- default is "object" unless
avoidObjectDetection()istruein which case it returns "undefined"
-
getClassName
Return the name of the class.This is typically the same name as the constructor. Classes extending ScriptableObject must implement this abstract method.
- Specified by:
getClassNamein interfaceScriptable
-
getThis
- Specified by:
getThisin classSlotMapOwner<Scriptable>
-
has
Returns true if the property index is defined.- Specified by:
hasin interfacePropHolder<Scriptable>- Specified by:
hasin interfaceScriptable- Overrides:
hasin classSlotMapOwner<Scriptable>- Parameters:
index- the numeric index for the propertystart- the object in which the lookup began- Returns:
- true if and only if the property was found in the object
- See Also:
-
get
Returns the value of the indexed property or NOT_FOUND.- Specified by:
getin interfacePropHolder<Scriptable>- Specified by:
getin interfaceScriptable- Overrides:
getin classSlotMapOwner<Scriptable>- Parameters:
index- the numeric index for the propertystart- the object in which the lookup began- Returns:
- the value of the property (may be null), or NOT_FOUND
- See Also:
-
put
Sets the value of the named property, creating it if need be.If the property was created using defineProperty, the appropriate setter method is called.
If the property's attributes include READONLY, no action is taken. This method will actually set the property in the start object.
- Specified by:
putin interfacePropHolder<Scriptable>- Specified by:
putin interfaceScriptable- Overrides:
putin classSlotMapOwner<Scriptable>- Parameters:
name- the name of the propertystart- the object whose property is being setvalue- value to set the property to- See Also:
-
putOwnProperty
Set the value of the named property, and return true if the property is actually defined and can be set. Subclasses of ScriptableObject should override this method, and not "put," for proper strict mode operation in the future.- Parameters:
isThrow- if true, throw an exception as if in strict mode
-
put
Sets the value of the indexed property, creating it if need be.- Specified by:
putin interfacePropHolder<Scriptable>- Specified by:
putin interfaceScriptable- Overrides:
putin classSlotMapOwner<Scriptable>- Parameters:
index- the numeric index for the propertystart- the object whose property is being setvalue- value to set the property to- See Also:
-
putOwnProperty
Set the value of the named property, and return true if the property is actually defined and can be set. Subclasses of ScriptableObject should override this method, and not "put," for proper strict mode operation in the future- Parameters:
isThrow- if true, throw an exception as if in strict mode
-
put
Implementation of put required by SymbolScriptable objects.- Specified by:
putin interfacePropHolder<Scriptable>- Specified by:
putin interfaceScriptable- Specified by:
putin interfaceSymbolScriptable- Overrides:
putin classSlotMapOwner<Scriptable>
-
putOwnProperty
Set the value of the named property, and return true if the property is actually defined and can be set. Subclasses of ScriptableObject should override this method, and not "put," for proper strict mode operation in the future- Parameters:
isThrow- if true, throw an exception as if in strict mode
-
delete
Removes a named property from the object.If the property is not found, or it has the PERMANENT attribute, no action is taken.
- Specified by:
deletein interfacePropHolder<Scriptable>- Specified by:
deletein interfaceScriptable- Overrides:
deletein classSlotMapOwner<Scriptable>- Parameters:
name- the name of the property- See Also:
-
delete
public void delete(int index) Removes the indexed property from the object.If the property is not found, or it has the PERMANENT attribute, no action is taken.
- Specified by:
deletein interfacePropHolder<Scriptable>- Specified by:
deletein interfaceScriptable- Overrides:
deletein classSlotMapOwner<Scriptable>- Parameters:
index- the numeric index for the property- See Also:
-
delete
Removes an object like the others, but using a Symbol as the key.- Specified by:
deletein interfacePropHolder<Scriptable>- Specified by:
deletein interfaceScriptable- Specified by:
deletein interfaceSymbolScriptable- Overrides:
deletein classSlotMapOwner<Scriptable>
-
checkSlotRemoval
protected static <T extends PropHolder<T>> Slot<T> checkSlotRemoval(Object key, int index, Slot<T> slot, CompoundOperationMap<T> compoundOp, SlotMapOwner<T> owner) -
putConst
Sets the value of the named const property, creating it if need be.If the property was created using defineProperty, the appropriate setter method is called.
If the property's attributes include READONLY, no action is taken. This method will actually set the property in the start object.
- Specified by:
putConstin interfaceConstProperties<Scriptable>- Parameters:
name- the name of the propertystart- the object whose property is being setvalue- value to set the property to- See Also:
-
defineConst
Description copied from interface:ConstPropertiesReserves a definition spot for a const. This will set up a definition of the const property, but set its value to undefined. The semantics of the start parameter is the same as for putConst.- Specified by:
defineConstin interfaceConstProperties<Scriptable>name- The name of the property.start- The object whose property is being reserved.
-
isConst
Returns true if the named property is defined as a const on this object.- Specified by:
isConstin interfaceConstProperties<Scriptable>- Parameters:
name- the name of the property- Returns:
- true if the named property is defined as a const, false otherwise.
-
setGetterOrSetter
Implement the legacy "__defineGetter__" and "__defineSetter__" methods. -
getGetterOrSetter
Get the getter or setter for a given property. Used by __lookupGetter__ and __lookupSetter__.- Parameters:
name- Name of the object. If nonnull, index must be 0.index- Index of the object. If nonzero, name must be null.scope- the current scope.isSetter- If true, return the setter, otherwise return the getter.- Returns:
- Undefined.instance if the property does not exist. Otherwise returns either the getter or the setter for the property, depending on the value of isSetter (may be undefined if unset).
- Throws:
IllegalArgumentException- if both name and index are nonnull and nonzero respectively.
-
getGetterOrSetter
Deprecated.since 1.7.14. Use the form with the scope to ensure consistent function prototype handling in all cases.Get the getter or setter for a given property. Used by __lookupGetter__ and __lookupSetter__.- Parameters:
name- Name of the object. If nonnull, index must be 0.index- Index of the object. If nonzero, name must be null.isSetter- If true, return the setter, otherwise return the getter.- Returns:
- Null if the property does not exist. Otherwise returns either the getter or the setter for the property, depending on the value of isSetter (may be undefined if unset).
- Throws:
IllegalArgumentException- if both name and index are nonnull and nonzero respectively.
-
isGetterOrSetter
Returns whether a property is a getter or a setter- Parameters:
name- property nameindex- property indexsetter- true to check for a setter, false for a getter- Returns:
- whether the property is a getter or a setter
-
isGetterOrSetter
protected boolean isGetterOrSetter(CompoundOperationMap map, String name, int index, boolean setter) -
setExternalArrayData
Attach the specified object to this object, and delegate all indexed property lookups to it. In other words, if the object has 3 elements, then an attempt to look up or modify "[0]", "[1]", or "[2]" will be delegated to this object. Additional indexed properties outside the range specified, and additional non-indexed properties, may still be added. The object specified must implement the ExternalArrayData interface.- Parameters:
array- the List to use for delegated property access. Set this to null to revert back to regular property access.- Since:
- 1.7.6
-
getExternalArrayData
Return the array that was previously set by the call to "setExternalArrayData".- Returns:
- the array, or null if it was never set
- Since:
- 1.7.6
-
getExternalArrayLength
This is a function used by setExternalArrayData to dynamically get the "length" property value. -
getPrototype
Returns the prototype of the object.- Specified by:
getPrototypein interfaceScriptable- Returns:
- the prototype
-
getAncestor
Description copied from interface:PropHolderGet the parent scope of the object.- Specified by:
getAncestorin interfacePropHolder<Scriptable>- Specified by:
getAncestorin interfaceScriptable- Returns:
- the parent scope
-
setPrototype
Sets the prototype of the object.- Specified by:
setPrototypein interfaceScriptable- Parameters:
m- the prototype to set
-
getParentScope
Returns the parent (enclosing) scope of the object.- Specified by:
getParentScopein interfaceScriptable- Returns:
- the parent scope
-
setParentScope
Sets the parent (enclosing) scope of the object.- Specified by:
setParentScopein interfaceScriptable- Parameters:
m- the parent scope to set
-
getAllIds
Returns an array of ids of all non-Symbol properties of the object.All non-Symbol properties, even those with attribute DONTENUM, are listed.
- Specified by:
getAllIdsin interfaceDebuggableObject- Returns:
- an array of java.lang.Objects with an entry for every non-Symbol property. Properties accessed via an integer index will have a corresponding Integer entry in the returned array. Properties accessed by a String will have a String entry in the returned array.
-
getDefaultValue
Implements the [[DefaultValue]] internal method.Note that the toPrimitive conversion is a no-op for every type other than Object, for which [[DefaultValue]] is called. See ECMA 9.1.
A
hintof null means "no hint".- Specified by:
getDefaultValuein interfaceScriptable- Parameters:
typeHint- the type hint- Returns:
- the default value for the object
See ECMA 8.6.2.6.
-
getDefaultValue
-
hasInstance
Implements the instanceof operator.This operator has been proposed to ECMA.
- Specified by:
hasInstancein interfaceScriptable- Parameters:
instance- The value that appeared on the LHS of the instanceof operator- Returns:
- true if "this" appears in value's prototype chain
-
avoidObjectDetection
public boolean avoidObjectDetection()Emulate the SpiderMonkey (and Firefox) feature of allowing custom objects to avoid detection by normal "object detection" code patterns. This is used to implement document.all. See bug 412247. This is an analog to JOF_DETECTING from SpiderMonkey; see bug 248549. Other than this special case, embeddings should return false.- Returns:
- true if this object should avoid object detection
- Since:
- 1.7R1
-
equivalentValues
Custom==operator. Must returnScriptable.NOT_FOUNDif this object does not have custom equality operator for the given value,Boolean.TRUEif this object is equivalent tovalue,Boolean.FALSEif this object is not equivalent tovalue.The default implementation returns Boolean.TRUE if
this == valueorScriptable.NOT_FOUNDotherwise. It indicates that by default custom equality is available only ifvalueisthisin which case true is returned. -
defineClass
public static <T extends Scriptable> void defineClass(VarScope scope, Class<T> clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException Defines JavaScript objects from a Java class that implements Scriptable.If the given class has a method
static void init(Context cx, VarScope scope, boolean sealed);
or its compatibility formstatic void init(Scriptable scope);
then it is invoked and no further initialization is done.However, if no such a method is found, then the class's constructors and methods are used to initialize a class in the following manner.
First, the zero-parameter constructor of the class is called to create the prototype. If no such constructor exists, a
EvaluatorExceptionis thrown.Next, all methods are scanned for special prefixes that indicate that they have special meaning for defining JavaScript objects. These special prefixes are
jsFunction_for a JavaScript functionjsStaticFunction_for a JavaScript function that is a property of the constructorjsGet_for a getter of a JavaScript propertyjsSet_for a setter of a JavaScript propertyjsConstructorfor a JavaScript function that is the constructor
If the method's name begins with "jsFunction_", a JavaScript function is created with a name formed from the rest of the Java method name following "jsFunction_". So a Java method named "jsFunction_foo" will define a JavaScript method "foo". Calling this JavaScript function will cause the Java method to be called. The parameters of the method must be of number and types as defined by the FunctionObject class. The JavaScript function is then added as a property of the prototype.
If the method's name begins with "jsStaticFunction_", it is handled similarly except that the resulting JavaScript function is added as a property of the constructor object. The Java method must be static.
If the method's name begins with "jsGet_" or "jsSet_", the method is considered to define a property. Accesses to the defined property will result in calls to these getter and setter methods. If no setter is defined, the property is defined as READONLY.
If the method's name is "jsConstructor", the method is considered to define the body of the constructor. Only one method of this name may be defined. You may use the varargs forms for constructors documented in
FunctionObject(String, Member, VarScope)If no method is found that can serve as constructor, a Java constructor will be selected to serve as the JavaScript constructor in the following manner. If the class has only one Java constructor, that constructor is used to define the JavaScript constructor. If the the class has two constructors, one must be the zero-argument constructor (otherwise an
EvaluatorExceptionwould have already been thrown when the prototype was to be created). In this case the Java constructor with one or more parameters will be used to define the JavaScript constructor. If the class has three or more constructors, anEvaluatorExceptionwill be thrown.Finally, if there is a method
static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype)it will be called to finish any initialization. Thescopeargument will be passed, along with the newly created constructor and the newly created prototype.- Parameters:
scope- The scope in which to define the constructor.clazz- The Java class to use to define the JavaScript objects and properties.- Throws:
IllegalAccessException- if access is not available to a reflected class memberInstantiationException- if unable to instantiate the named classInvocationTargetException- if an exception is thrown during execution of methods of the named class- See Also:
-
defineClass
public static <T extends Scriptable> void defineClass(VarScope scope, Class<T> clazz, boolean sealed) throws IllegalAccessException, InstantiationException, InvocationTargetException Defines JavaScript objects from a Java class, optionally allowing sealing.Similar to
defineClass(Scriptable scope, Class clazz)except that sealing is allowed. An object that is sealed cannot have properties added or removed. Note that sealing is not allowed in the current ECMA/ISO language specification, but is likely for the next version.- Parameters:
scope- The scope in which to define the constructor.clazz- The Java class to use to define the JavaScript objects and properties. The class must implement Scriptable.sealed- Whether or not to create sealed standard objects that cannot be modified.- Throws:
IllegalAccessException- if access is not available to a reflected class memberInstantiationException- if unable to instantiate the named classInvocationTargetException- if an exception is thrown during execution of methods of the named class- Since:
- 1.4R3
-
defineClass
public static <T extends Scriptable> String defineClass(VarScope scope, Class<T> clazz, boolean sealed, boolean mapInheritance) throws IllegalAccessException, InstantiationException, InvocationTargetException Defines JavaScript objects from a Java class, optionally allowing sealing and mapping of Java inheritance to JavaScript prototype-based inheritance.Similar to
defineClass(Scriptable scope, Class clazz)except that sealing and inheritance mapping are allowed. An object that is sealed cannot have properties added or removed. Note that sealing is not allowed in the current ECMA/ISO language specification, but is likely for the next version.- Parameters:
scope- The scope in which to define the constructor.clazz- The Java class to use to define the JavaScript objects and properties. The class must implement Scriptable.sealed- Whether or not to create sealed standard objects that cannot be modified.mapInheritance- Whether or not to map Java inheritance to JavaScript prototype-based inheritance.- Returns:
- the class name for the prototype of the specified class
- Throws:
IllegalAccessException- if access is not available to a reflected class memberInstantiationException- if unable to instantiate the named classInvocationTargetException- if an exception is thrown during execution of methods of the named class- Since:
- 1.6R2
-
defineProperty
public static <T extends PropHolder<T>> void defineProperty(T destination, String propertyName, Object value, int attributes) Utility method to add properties to arbitrary Scriptable object. If destination is instance of ScriptableObject, calls defineProperty there, otherwise calls put in destination ignoring attributes- Parameters:
destination- ScriptableObject to define the property onpropertyName- the name of the property to define.value- the initial value of the propertyattributes- the attributes of the JavaScript property
-
defineProperty
public static <T extends PropHolder<T>> void defineProperty(T destination, SymbolKey propertyName, Object value, int attributes) -
defineProperty
public void defineProperty(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) Utility method to add lambda properties to arbitrary Scriptable object.- Parameters:
scope- ScriptableObject to define the property onname- the name of the property to define.length- the arity of the functiontarget- an object that implements the function in Java. Since Callable is a single-function interface this will typically be implemented as a lambda.attributes- the attributes of the JavaScript propertypropertyAttributes- Sets the attributes of the "name", "length", and "arity" properties of the internal LambdaFunction, which differ for many * native objects.
-
defineProperty
-
defineBuiltinProperty
public void defineBuiltinProperty(VarScope scope, String name, int length, SerializableCallable target) -
defineBuiltinProperty
public void defineBuiltinProperty(VarScope scope, String name, int length, SerializableCallable target, int attributes, int propertyAttributes) -
defineConstProperty
public static <T extends PropHolder<T>> void defineConstProperty(Context cx, T destination, String propertyName) Utility method to add properties to arbitrary Scriptable object. If destination is instance of ScriptableObject, calls defineProperty there, otherwise calls put in destination ignoring attributes- Parameters:
destination- ScriptableObject to define the property onpropertyName- the name of the property to define.
-
defineProperty
Define a JavaScript property with getter and setter side effects.If the setter is not found, the attribute READONLY is added to the given attributes.
The getter must be a method with zero parameters, and the setter, if found, must be a method with one parameter.
- Parameters:
propertyName- the name of the property to define. This name also affects the name of the setter and getter to search for. If the propertyId is "foo", thenclazzwill be searched for "getFoo" and "setFoo" methods.clazz- the Java class to search for the getter and setterattributes- the attributes of the JavaScript property- See Also:
-
defineProperty
public void defineProperty(VarScope scope, String propertyName, Object delegateTo, Method getter, Method setter, int attributes) Define a JavaScript property.Use this method only if you wish to define getters and setters for a given property in a ScriptableObject. To create a property without special getter or setter side effects, use
defineProperty(String,int).If
setteris null, the attribute READONLY is added to the given attributes.Several forms of getters or setters are allowed. In all cases the type of the value parameter can be any one of the following types: Object, String, boolean, Scriptable, byte, short, int, long, float, or double. The runtime will perform appropriate conversions based upon the type of the parameter (see description in FunctionObject). The first forms are nonstatic methods of the class referred to by 'this':
Object getFoo(); void setFoo(SomeType value);
Next are static methods that may be of any class; the object whose property is being accessed is passed in as an extra argument:static Object getFoo(Scriptable obj); static void setFoo(Scriptable obj, SomeType value);
Finally, it is possible to delegate to another object entirely using thedelegateToparameter. In this case the methods are nonstatic methods of the class delegated to, and the object whose property is being accessed is passed in as an extra argument:Object getFoo(Scriptable obj); void setFoo(Scriptable obj, SomeType value);
- Parameters:
propertyName- the name of the property to define.delegateTo- an object to call the getter and setter methods on, or null, depending on the form used above.getter- the method to invoke to get the value of the propertysetter- the method to invoke to set the value of the propertyattributes- the attributes of the JavaScript property
-
defineOwnProperties
Defines one or more properties on this object.- Parameters:
cx- the current Contextprops- a map of property ids to property descriptors
-
defineOwnProperty
Defines a property on an object.- Parameters:
cx- the current Contextid- the name/index of the propertydesc- the new property descriptor, as described in 8.6.1
-
defineOwnProperty
-
defineOwnProperty
protected boolean defineOwnProperty(Context cx, Object id, ScriptableObject.DescriptorInfo desc, boolean checkValid) Defines a property on an object.Based on [[DefineOwnProperty]] from 8.12.10 of the spec. see [[DefineOwnProperty]]
- Parameters:
cx- the current Contextid- the name/index of the propertydesc- the new property descriptor, as described in 8.6.1checkValid- whether to perform validity checks- Returns:
- always true at the moment
-
defineProperty
public void defineProperty(String name, Supplier<Object> getter, Consumer<Object> setter, int attributes) Define a property on this object that is implemented using lambda functions. If a property with the same name already exists, then it will be replaced. This property will appear to the JavaScript user exactly like any other property -- unlike Function properties and those based on reflection, the property descriptor will only reflect the value as defined by this function.- Parameters:
name- the name of the functiongetter- a function that returns the value of the property. If null, then the previous version of the value will be returned.setter- a function that sets the value of the property. If null, then the value will be set directly and may not be retrieved by the getter.attributes- the attributes to set on the property
-
defineProperty
public void defineProperty(Context cx, VarScope scope, String name, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) Define a property on this object that is implemented using lambda functions accepting Scriptable `this` object as first parameter. Unlike with `defineProperty(String name, Supplier<Object> getter, Consumer<Object> setter, int attributes)` where getter and setter need to have access to target object instance, this allows for defining properties on LambdaConstructor prototype providing getter and setter logic with java instance methods. If a property with the same name already exists, then it will be replaced. This property will appear to the JavaScript user exactly like descriptor with a getter and setter, just as if they had been defined in JavaScript using Object.defineOwnProperty.- Parameters:
name- the name of the propertygetter- a function that given Scriptable `this` returns the value of the property. If null, throws typeErrorsetter- a function that Scriptable `this` and a value sets the value of the property, by calling appropriate method on `this`. If null, then the value will be set directly and may not be retrieved by the getter.attributes- the attributes to set on the property
-
defineProperty
public void defineProperty(Context cx, VarScope scope, String name, ScriptableObject.LambdaGetterFunction getter, int attributes) -
defineProperty
public void defineProperty(Context cx, VarScope scope, Symbol key, ScriptableObject.LambdaGetterFunction getter, ScriptableObject.LambdaSetterFunction setter, int attributes) -
checkPropertyDefinition
-
checkPropertyDefinition
-
checkPropertyChangeForSlot
-
checkPropertyChangeForSlot
protected final void checkPropertyChangeForSlot(Object id, Slot<?> current, ScriptableObject.DescriptorInfo info) -
isTrue
-
isFalse
-
sameValue
Implements SameValue as described in ES5 9.12, additionally checking if new value is defined.- Parameters:
newValue- the new valuecurrentValue- the current value- Returns:
- true if values are the same as defined by ES5 9.12
-
applyDescriptorToAttributeBitset
-
isDataDescriptor
Implements IsDataDescriptor as described in ES5 8.10.2- Parameters:
desc- a property descriptor- Returns:
- true if this is a data descriptor.
-
isAccessorDescriptor
Implements IsAccessorDescriptor as described in ES5 8.10.1- Parameters:
desc- a property descriptor- Returns:
- true if this is an accessor descriptor.
-
isAccessorDescriptor
-
isGenericDescriptor
Implements IsGenericDescriptor as described in ES5 8.10.3- Parameters:
desc- a property descriptor- Returns:
- true if this is a generic descriptor.
-
isGenericDescriptor
-
ensureScriptable
-
ensureSymbolScriptable
-
ensureScriptableObject
-
ensureScriptableObjectButNotSymbol
-
defineFunctionProperties
public void defineFunctionProperties(VarScope scope, String[] names, Class<?> clazz, int attributes) Search for names in a class, adding the resulting methods as properties.Uses reflection to find the methods of the given names. Then FunctionObjects are constructed from the methods found, and are added to this object as properties with the given names.
- Parameters:
scope- the declaration scope for the created function.names- the names of the Methods to add as function propertiesclazz- the class to search for the Methodsattributes- the attributes of the new properties- See Also:
-
getObjectPrototype
Get the Object.prototype property. See ECMA 15.2.4.- Parameters:
scope- an object in the scope chain
-
getFunctionPrototype
Get the Function.prototype property. See ECMA 15.3.4.- Parameters:
scope- an object in the scope chain
-
getGeneratorFunctionPrototype
-
getArrayPrototype
-
getClassPrototype
Get the prototype for the named class.For example,
getClassPrototype(s, "Date")will first walk up the parent chain to find the outermost scope, then will search that scope for the Date constructor, and then will return Date.prototype. If any of the lookups fail, or the prototype is not a JavaScript object, then null will be returned.- Parameters:
scope- an object in the scope chainclassName- the name of the constructor- Returns:
- the prototype for the named class, or null if it cannot be found.
-
getClassPrototype
-
getTopLevelScope
Get the global scope.Walks the parent scope chain to find an object with a null parent scope (the global object).
- Parameters:
obj- a JavaScript object- Returns:
- the corresponding global scope
-
isExtensible
public boolean isExtensible() -
preventExtensions
public boolean preventExtensions() -
sealObject
public void sealObject()Seal this object.It is an error to add properties to or delete properties from a sealed object. It is possible to change the value of an existing property. Once an object is sealed it may not be unsealed.
- Overrides:
sealObjectin classSlotMapOwner<Scriptable>- Since:
- 1.4R3
-
checkNotSealed
-
getProperty
Gets a named property from an object or any object in its prototype chain.Searches the prototype chain for a property named
name.- Parameters:
obj- a JavaScript objectname- a property name- Returns:
- the value of a property with name
namefound inobjor any object in its prototype chain, orScriptable.NOT_FOUNDif not found - Since:
- 1.5R2
-
getProperty
-
getProperty
-
getSuperProperty
Gets a named property from super, walking the super's prototype chain, but passing the correct "this" to getter slots. -
getProperty
This is a version of getProperty that works with Symbols. -
getSuperProperty
This is a version of getSuperProperty that works with Symbols. -
getTypedProperty
Gets an indexed property from an object or any object in its prototype chain and coerces it to the requested Java type.Searches the prototype chain for a property with integral index
index. Note that if you wish to look for properties with numerical but non-integral indicies, you should use getProperty(Scriptable,String) with the string value of the index.- Parameters:
s- a JavaScript objectindex- an integral indextype- the required Java type of the result- Returns:
- the value of a property with name
namefound inobjor any object in its prototype chain, or null if not found. Note that it does not returnScriptable.NOT_FOUNDas it can ordinarily not be converted to most of the types. - Since:
- 1.7R3
-
getProperty
Gets an indexed property from an object or any object in its prototype chain.Searches the prototype chain for a property with integral index
index. Note that if you wish to look for properties with numerical but non-integral indicies, you should use getProperty(Scriptable,String) with the string value of the index.- Parameters:
obj- a JavaScript objectindex- an integral index- Returns:
- the value of a property with index
indexfound inobjor any object in its prototype chain, orScriptable.NOT_FOUNDif not found - Since:
- 1.5R2
-
getSuperProperty
Gets an indexed property from super, walking the super's prototype chain, but passing the correct "this" to getter slots. -
getTypedProperty
Gets a named property from an object or any object in its prototype chain and coerces it to the requested Java type.Searches the prototype chain for a property named
name.- Parameters:
s- a JavaScript objectname- a property nametype- the required Java type of the result- Returns:
- the value of a property with name
namefound inobjor any object in its prototype chain, or null if not found. Note that it does not returnScriptable.NOT_FOUNDas it can ordinarily not be converted to most of the types. - Since:
- 1.7R3
-
hasProperty
Returns whether a named property is defined in an object or any object in its prototype chain.Searches the prototype chain for a property named
name.- Parameters:
obj- a JavaScript objectname- a property name- Returns:
- the true if property was found
- Since:
- 1.5R2
-
hasProperty
-
redefineProperty
If hasProperty(obj, name) would return true, then if the property that was found is compatible with the new property, this method just returns. If the property is not compatible, then an exception is thrown.A property redefinition is incompatible if the first definition was a const declaration or if this one is. They are compatible only if neither was const.
-
redefineProperty
-
hasProperty
Returns whether an indexed property is defined in an object or any object in its prototype chain.Searches the prototype chain for a property with index
index.- Parameters:
obj- a JavaScript objectindex- a property index- Returns:
- the true if property was found
- Since:
- 1.5R2
-
hasProperty
A version of hasProperty for properties with Symbol keys. -
putProperty
Puts a named property in an object or in an object in its prototype chain.Searches for the named property in the prototype chain. If it is found, the value of the property in
objis changed through a call toScriptable.put(String, Scriptable, Object)on the prototype passingobjas thestartargument. This allows the prototype to veto the property setting in case the prototype defines the property with [[ReadOnly]] attribute. If the property is not found, it is added inobj.- Parameters:
obj- a JavaScript objectname- a property namevalue- any JavaScript value accepted by Scriptable.put- Since:
- 1.5R2
-
putProperty
-
putSuperProperty
public static void putSuperProperty(Scriptable superObj, Scriptable thisObj, String name, Object value) Variant of putProperty to handle super.name = value -
putProperty
This is a version of putProperty for Symbol keys. -
putSuperProperty
public static void putSuperProperty(Scriptable superObj, Scriptable thisObj, Symbol key, Object value) Variant of putProperty to handle super[key] = value where key is a symbol -
putConstProperty
Puts a named property in an object or in an object in its prototype chain.Searches for the named property in the prototype chain. If it is found, the value of the property in
objis changed through a call toScriptable.put(String, Scriptable, Object)on the prototype passingobjas thestartargument. This allows the prototype to veto the property setting in case the prototype defines the property with [[ReadOnly]] attribute. If the property is not found, it is added inobj.- Parameters:
obj- a JavaScript objectname- a property namevalue- any JavaScript value accepted by Scriptable.put- Since:
- 1.5R2
-
putConstProperty
-
putProperty
Puts an indexed property in an object or in an object in its prototype chain.Searches for the indexed property in the prototype chain. If it is found, the value of the property in
objis changed through a call toScriptable.put(int, Scriptable, Object)on the prototype passingobjas thestartargument. This allows the prototype to veto the property setting in case the prototype defines the property with [[ReadOnly]] attribute. If the property is not found, it is added inobj.- Parameters:
obj- a JavaScript objectindex- a property indexvalue- any JavaScript value accepted by Scriptable.put- Since:
- 1.5R2
-
putSuperProperty
public static void putSuperProperty(Scriptable superObj, Scriptable thisObj, int index, Object value) Variant of putProperty to handle super[index] = value where index is integer -
deleteProperty
Removes the property from an object or its prototype chain.Searches for a property with
namein obj or its prototype chain. If it is found, the object's delete method is called.- Parameters:
obj- a JavaScript objectname- a property name- Returns:
- true if the property doesn't exist or was successfully removed
- Since:
- 1.5R2
-
deleteProperty
Removes the property from an object or its prototype chain.Searches for a property with
indexin obj or its prototype chain. If it is found, the object's delete method is called.- Parameters:
obj- a JavaScript objectindex- a property index- Returns:
- true if the property doesn't exist or was successfully removed
- Since:
- 1.5R2
-
deleteProperty
A version of deleteProperty for properties with Symbol keys. -
getPropertyIds
Returns an array of all ids from an object and its prototypes.- Parameters:
obj- a JavaScript object- Returns:
- an array of all ids from all object in the prototype chain. If a given id occurs multiple times in the prototype chain, it will occur only once in this list.
- Since:
- 1.5R2
-
callMethod
Call a method of an object.- Parameters:
obj- the JavaScript objectmethodName- the name of the function propertyargs- the arguments for the call- See Also:
-
callMethod
Call a method of an object.- Parameters:
cx- the Context object associated with the current thread.obj- the JavaScript objectmethodName- the name of the function propertyargs- the arguments for the call
-
getTopScopeValue
Get arbitrary application-specific value associated with the top scope of the given scope. The method first callsgetTopLevelScope(VarScope scope)and then searches the prototype chain of the top scope for the first object containing the associated value with the given key.- Parameters:
scope- the starting scope.key- key object to select particular value.- See Also:
-
getOwnPropertyDescriptor
-
querySlot
-
size
public int size() -
isEmpty
public boolean isEmpty() -
get
-
defineBuiltInProperty
public static <T extends ScriptableObject> void defineBuiltInProperty(T owner, int attributes, BuiltInSlot.Descriptor<T> descriptor) -
ensureType
-