public abstract class ScriptableObject extends java.lang.Object implements Scriptable, java.io.Serializable, DebuggableObject, ConstProperties
Various properties and methods of JavaScript objects can be conveniently defined using methods of ScriptableObject.
Classes extending ScriptableObject must define the getClassName method.
Scriptable
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
CONST |
static int |
DONTENUM
Property attribute indicating property is not enumerated.
|
static int |
EMPTY
The empty property attribute.
|
static int |
PERMANENT
Property attribute indicating property cannot be deleted.
|
static int |
READONLY
Property attribute indicating assignment to this property is ignored.
|
static int |
UNINITIALIZED_CONST
Property attribute indicating that this is a const property that has not
been assigned yet.
|
NOT_FOUND
Constructor and Description |
---|
ScriptableObject() |
ScriptableObject(Scriptable scope,
Scriptable prototype) |
Modifier and Type | Method and Description |
---|---|
protected int |
applyDescriptorToAttributeBitset(int attributes,
ScriptableObject desc) |
java.lang.Object |
associateValue(java.lang.Object key,
java.lang.Object value)
Associate arbitrary application-specific value with this object.
|
boolean |
avoidObjectDetection()
Emulate the SpiderMonkey (and Firefox) feature of allowing
custom objects to avoid detection by normal "object detection"
code patterns.
|
protected static ScriptableObject |
buildDataDescriptor(Scriptable scope,
java.lang.Object value,
int attributes) |
static java.lang.Object |
callMethod(Context cx,
Scriptable obj,
java.lang.String methodName,
java.lang.Object[] args)
Call a method of an object.
|
static java.lang.Object |
callMethod(Scriptable obj,
java.lang.String methodName,
java.lang.Object[] args)
Call a method of an object.
|
protected void |
checkPropertyChange(java.lang.String id,
ScriptableObject current,
ScriptableObject desc) |
protected void |
checkPropertyDefinition(ScriptableObject desc) |
static <T extends Scriptable> |
defineClass(Scriptable scope,
java.lang.Class<T> clazz)
Defines JavaScript objects from a Java class that implements Scriptable.
|
static <T extends Scriptable> |
defineClass(Scriptable scope,
java.lang.Class<T> clazz,
boolean sealed)
Defines JavaScript objects from a Java class, optionally
allowing sealing.
|
static <T extends Scriptable> |
defineClass(Scriptable scope,
java.lang.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.
|
void |
defineConst(java.lang.String name,
Scriptable start)
Reserves a definition spot for a const.
|
static void |
defineConstProperty(Scriptable destination,
java.lang.String propertyName)
Utility method to add properties to arbitrary Scriptable object.
|
void |
defineFunctionProperties(java.lang.String[] names,
java.lang.Class<?> clazz,
int attributes)
Search for names in a class, adding the resulting methods
as properties.
|
void |
defineOwnProperties(Context cx,
ScriptableObject props)
Defines one or more properties on this object.
|
void |
defineOwnProperty(Context cx,
java.lang.Object id,
ScriptableObject desc)
Defines a property on an object.
|
protected void |
defineOwnProperty(Context cx,
java.lang.Object id,
ScriptableObject desc,
boolean checkValid)
Defines a property on an object.
|
static void |
defineProperty(Scriptable destination,
java.lang.String propertyName,
java.lang.Object value,
int attributes)
Utility method to add properties to arbitrary Scriptable object.
|
void |
defineProperty(java.lang.String propertyName,
java.lang.Class<?> clazz,
int attributes)
Define a JavaScript property with getter and setter side effects.
|
void |
defineProperty(java.lang.String propertyName,
java.lang.Object value,
int attributes)
Define a JavaScript property.
|
void |
defineProperty(java.lang.String propertyName,
java.lang.Object delegateTo,
java.lang.reflect.Method getter,
java.lang.reflect.Method setter,
int attributes)
Define a JavaScript property.
|
void |
delete(int index)
Removes the indexed property from the object.
|
void |
delete(java.lang.String name)
Removes a named property from the object.
|
static boolean |
deleteProperty(Scriptable obj,
int index)
Removes the property from an object or its prototype chain.
|
static boolean |
deleteProperty(Scriptable obj,
java.lang.String name)
Removes the property from an object or its prototype chain.
|
protected static Scriptable |
ensureScriptable(java.lang.Object arg) |
protected static ScriptableObject |
ensureScriptableObject(java.lang.Object arg) |
protected java.lang.Object |
equivalentValues(java.lang.Object value)
Custom == operator.
|
java.lang.Object |
get(int index,
Scriptable start)
Returns the value of the indexed property or NOT_FOUND.
|
java.lang.Object |
get(java.lang.Object key) |
java.lang.Object |
get(java.lang.String name,
Scriptable start)
Returns the value of the named property or NOT_FOUND.
|
java.lang.Object[] |
getAllIds()
Returns an array of ids for the properties of the object.
|
static Scriptable |
getArrayPrototype(Scriptable scope) |
java.lang.Object |
getAssociatedValue(java.lang.Object key)
Get arbitrary application-specific value associated with this object.
|
int |
getAttributes(int index)
Get the attributes of an indexed property.
|
int |
getAttributes(int index,
Scriptable start)
Deprecated.
Use
getAttributes(int index) . The engine always
ignored the start argument. |
int |
getAttributes(java.lang.String name)
Get the attributes of a named property.
|
int |
getAttributes(java.lang.String name,
Scriptable start)
Deprecated.
Use
getAttributes(String name) . The engine always
ignored the start argument. |
abstract java.lang.String |
getClassName()
Return the name of the class.
|
static Scriptable |
getClassPrototype(Scriptable scope,
java.lang.String className)
Get the prototype for the named class.
|
java.lang.Object |
getDefaultValue(java.lang.Class<?> typeHint)
Implements the [[DefaultValue]] internal method.
|
static java.lang.Object |
getDefaultValue(Scriptable object,
java.lang.Class<?> typeHint) |
static Scriptable |
getFunctionPrototype(Scriptable scope)
Get the Function.prototype property.
|
java.lang.Object |
getGetterOrSetter(java.lang.String name,
int index,
boolean isSetter)
Get the getter or setter for a given property.
|
java.lang.Object[] |
getIds()
Returns an array of ids for the properties of the object.
|
static Scriptable |
getObjectPrototype(Scriptable scope)
Get the Object.prototype property.
|
protected ScriptableObject |
getOwnPropertyDescriptor(Context cx,
java.lang.Object id) |
Scriptable |
getParentScope()
Returns the parent (enclosing) scope of the object.
|
static java.lang.Object |
getProperty(Scriptable obj,
int index)
Gets an indexed property from an object or any object in its prototype chain.
|
static java.lang.Object |
getProperty(Scriptable obj,
java.lang.String name)
Gets a named property from an object or any object in its prototype chain.
|
static java.lang.Object[] |
getPropertyIds(Scriptable obj)
Returns an array of all ids from an object and its prototypes.
|
Scriptable |
getPrototype()
Returns the prototype of the object.
|
protected org.mozilla.javascript.ScriptableObject.Slot |
getSlot(Context cx,
java.lang.Object id,
int accessType) |
static Scriptable |
getTopLevelScope(Scriptable obj)
Get the global scope.
|
static java.lang.Object |
getTopScopeValue(Scriptable scope,
java.lang.Object key)
Get arbitrary application-specific value associated with the top scope
of the given scope.
|
static <T> T |
getTypedProperty(Scriptable s,
int index,
java.lang.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> T |
getTypedProperty(Scriptable s,
java.lang.String name,
java.lang.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.
|
java.lang.String |
getTypeOf()
Gets the value that will be returned by calling the typeof operator on this object.
|
boolean |
has(int index,
Scriptable start)
Returns true if the property index is defined.
|
boolean |
has(java.lang.String name,
Scriptable start)
Returns true if the named property is defined.
|
boolean |
hasInstance(Scriptable instance)
Implements the instanceof operator.
|
static boolean |
hasProperty(Scriptable obj,
int index)
Returns whether an indexed property is defined in an object or any object
in its prototype chain.
|
static boolean |
hasProperty(Scriptable obj,
java.lang.String name)
Returns whether a named property is defined in an object or any object
in its prototype chain.
|
protected boolean |
isAccessorDescriptor(ScriptableObject desc)
Implements IsAccessorDescriptor as described in ES5 8.10.1
|
boolean |
isConst(java.lang.String name)
Returns true if the named property is defined as a const on this object.
|
protected boolean |
isDataDescriptor(ScriptableObject desc)
Implements IsDataDescriptor as described in ES5 8.10.2
|
boolean |
isEmpty() |
boolean |
isExtensible() |
protected static boolean |
isFalse(java.lang.Object value) |
protected boolean |
isGenericDescriptor(ScriptableObject desc)
Implements IsGenericDescriptor as described in ES5 8.10.3
|
protected boolean |
isGetterOrSetter(java.lang.String name,
int index,
boolean setter)
Returns whether a property is a getter or a setter
|
boolean |
isSealed()
Return true if this object is sealed.
|
protected static boolean |
isTrue(java.lang.Object value) |
void |
preventExtensions() |
void |
put(int index,
Scriptable start,
java.lang.Object value)
Sets the value of the indexed property, creating it if need be.
|
void |
put(java.lang.String name,
Scriptable start,
java.lang.Object value)
Sets the value of the named property, creating it if need be.
|
void |
putConst(java.lang.String name,
Scriptable start,
java.lang.Object value)
Sets the value of the named const property, creating it if need be.
|
static void |
putConstProperty(Scriptable obj,
java.lang.String name,
java.lang.Object value)
Puts a named property in an object or in an object in its prototype chain.
|
static void |
putProperty(Scriptable obj,
int index,
java.lang.Object value)
Puts an indexed property in an object or in an object in its prototype chain.
|
static void |
putProperty(Scriptable obj,
java.lang.String name,
java.lang.Object value)
Puts a named property in an object or in an object in its prototype chain.
|
static void |
redefineProperty(Scriptable obj,
java.lang.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.
|
protected boolean |
sameValue(java.lang.Object newValue,
java.lang.Object currentValue)
Implements SameValue as described in ES5 9.12, additionally checking
if new value is defined.
|
void |
sealObject()
Seal this object.
|
void |
setAttributes(int index,
int attributes)
Set the attributes of an indexed property.
|
void |
setAttributes(int index,
Scriptable start,
int attributes)
Deprecated.
Use
setAttributes(int index, int attributes) .
The engine always ignored the start argument. |
void |
setAttributes(java.lang.String name,
int attributes)
Set the attributes of a named property.
|
void |
setAttributes(java.lang.String name,
Scriptable start,
int attributes)
Deprecated.
Use
setAttributes(String name, int attributes) .
The engine always ignored the start argument. |
void |
setGetterOrSetter(java.lang.String name,
int index,
Callable getterOrSetter,
boolean isSetter)
XXX: write docs.
|
void |
setParentScope(Scriptable m)
Sets the parent (enclosing) scope of the object.
|
void |
setPrototype(Scriptable m)
Sets the prototype of the object.
|
int |
size() |
public static final int EMPTY
public static final int READONLY
public static final int DONTENUM
public static final int PERMANENT
public static final int UNINITIALIZED_CONST
public static final int CONST
public ScriptableObject()
public ScriptableObject(Scriptable scope, Scriptable prototype)
protected static ScriptableObject buildDataDescriptor(Scriptable scope, java.lang.Object value, int attributes)
public java.lang.String getTypeOf()
avoidObjectDetection()
is true
in which
case it returns "undefined"public abstract java.lang.String getClassName()
getClassName
in interface Scriptable
public boolean has(java.lang.String name, Scriptable start)
has
in interface Scriptable
name
- the name of the propertystart
- the object in which the lookup beganScriptable.get(String, Scriptable)
,
getProperty(Scriptable, String)
public boolean has(int index, Scriptable start)
has
in interface Scriptable
index
- the numeric index for the propertystart
- the object in which the lookup beganScriptable.get(int, Scriptable)
,
getProperty(Scriptable, int)
public java.lang.Object get(java.lang.String name, Scriptable start)
get
in interface Scriptable
name
- the name of the propertystart
- the object in which the lookup beganContext.getUndefinedValue()
public java.lang.Object get(int index, Scriptable start)
get
in interface Scriptable
index
- the numeric index for the propertystart
- the object in which the lookup beganScriptable.get(String,Scriptable)
public void put(java.lang.String name, Scriptable start, java.lang.Object value)
If the property's attributes include READONLY, no action is taken. This method will actually set the property in the start object.
put
in interface Scriptable
name
- the name of the propertystart
- the object whose property is being setvalue
- value to set the property toScriptable.has(String, Scriptable)
,
Scriptable.get(String, Scriptable)
,
putProperty(Scriptable, String, Object)
,
Context.toObject(Object, Scriptable)
public void put(int index, Scriptable start, java.lang.Object value)
put
in interface Scriptable
index
- the numeric index for the propertystart
- the object whose property is being setvalue
- value to set the property toScriptable.has(int, Scriptable)
,
Scriptable.get(int, Scriptable)
,
putProperty(Scriptable, int, Object)
,
Context.toObject(Object, Scriptable)
public void delete(java.lang.String name)
delete
in interface Scriptable
name
- the name of the propertyScriptable.get(String, Scriptable)
,
deleteProperty(Scriptable, String)
public void delete(int index)
delete
in interface Scriptable
index
- the numeric index for the propertyScriptable.get(int, Scriptable)
,
deleteProperty(Scriptable, int)
public void putConst(java.lang.String name, Scriptable start, java.lang.Object value)
If the property's attributes include READONLY, no action is taken. This method will actually set the property in the start object.
putConst
in interface ConstProperties
name
- the name of the propertystart
- the object whose property is being setvalue
- value to set the property toScriptable.has(String, Scriptable)
,
Scriptable.get(String, Scriptable)
,
putProperty(Scriptable, String, Object)
,
Context.toObject(Object, Scriptable)
public void defineConst(java.lang.String name, Scriptable start)
ConstProperties
defineConst
in interface ConstProperties
name
- The name of the property.start
- The object whose property is being reserved.public boolean isConst(java.lang.String name)
isConst
in interface ConstProperties
name
- public final int getAttributes(java.lang.String name, Scriptable start)
getAttributes(String name)
. The engine always
ignored the start argument.public final int getAttributes(int index, Scriptable start)
getAttributes(int index)
. The engine always
ignored the start argument.public final void setAttributes(java.lang.String name, Scriptable start, int attributes)
setAttributes(String name, int attributes)
.
The engine always ignored the start argument.public void setAttributes(int index, Scriptable start, int attributes)
setAttributes(int index, int attributes)
.
The engine always ignored the start argument.public int getAttributes(java.lang.String name)
name
as defined for has
.name
- the identifier for the propertyEvaluatorException
- if the named property is not foundhas(String, Scriptable)
,
READONLY
,
DONTENUM
,
PERMANENT
,
EMPTY
public int getAttributes(int index)
index
- the numeric index for the propertyEvaluatorException
- if the named property is not found
is not foundhas(String, Scriptable)
,
READONLY
,
DONTENUM
,
PERMANENT
,
EMPTY
public void setAttributes(java.lang.String name, int attributes)
name
as defined for has
.The possible attributes are READONLY, DONTENUM, and PERMANENT. Combinations of attributes are expressed by the bitwise OR of attributes. EMPTY is the state of no attributes set. Any unused bits are reserved for future use.
name
- the name of the propertyattributes
- the bitset of attributesEvaluatorException
- if the named property is not foundScriptable.has(String, Scriptable)
,
READONLY
,
DONTENUM
,
PERMANENT
,
EMPTY
public void setAttributes(int index, int attributes)
index
- the numeric index for the propertyattributes
- the bitset of attributesEvaluatorException
- if the named property is not foundScriptable.has(String, Scriptable)
,
READONLY
,
DONTENUM
,
PERMANENT
,
EMPTY
public void setGetterOrSetter(java.lang.String name, int index, Callable getterOrSetter, boolean isSetter)
public java.lang.Object getGetterOrSetter(java.lang.String name, int index, boolean isSetter)
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.java.lang.IllegalArgumentException
- if both name and index are nonnull
and nonzero respectively.protected boolean isGetterOrSetter(java.lang.String name, int index, boolean setter)
name
- property nameindex
- property indexsetter
- true to check for a setter, false for a getterpublic Scriptable getPrototype()
getPrototype
in interface Scriptable
public void setPrototype(Scriptable m)
setPrototype
in interface Scriptable
m
- the prototype to setpublic Scriptable getParentScope()
getParentScope
in interface Scriptable
public void setParentScope(Scriptable m)
setParentScope
in interface Scriptable
m
- the parent scope to setpublic java.lang.Object[] getIds()
Any properties with the attribute DONTENUM are not listed.
getIds
in interface Scriptable
public java.lang.Object[] getAllIds()
All properties, even those with attribute DONTENUM, are listed.
getAllIds
in interface DebuggableObject
public java.lang.Object getDefaultValue(java.lang.Class<?> typeHint)
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 hint
of null means "no hint".
getDefaultValue
in interface Scriptable
typeHint
- the type hintpublic static java.lang.Object getDefaultValue(Scriptable object, java.lang.Class<?> typeHint)
public boolean hasInstance(Scriptable instance)
This operator has been proposed to ECMA.
hasInstance
in interface Scriptable
instance
- The value that appeared on the LHS of the instanceof
operatorpublic boolean avoidObjectDetection()
protected java.lang.Object equivalentValues(java.lang.Object value)
Scriptable.NOT_FOUND
if this object does not
have custom equality operator for the given value,
Boolean.TRUE if this object is equivalent to value,
Boolean.FALSE if this object is not equivalent to
value.
The default implementation returns Boolean.TRUE
if this == value or Scriptable.NOT_FOUND
otherwise.
It indicates that by default custom equality is available only if
value is this in which case true is returned.
public static <T extends Scriptable> void defineClass(Scriptable scope, java.lang.Class<T> clazz) throws java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
static void init(Context cx, Scriptable scope, boolean sealed);or its compatibility form
static 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 EvaluatorException
is 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 function
jsStaticFunction_
for a JavaScript function that
is a property of the constructor
jsGet_
for a getter of a JavaScript property
jsSet_
for a setter of a JavaScript property
jsConstructor
for 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.FunctionObject(String, Member, Scriptable)
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
EvaluatorException
would 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, an EvaluatorException
will 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. The
scope
argument will be passed, along with the newly created constructor and
the newly created prototype.scope
- The scope in which to define the constructor.clazz
- The Java class to use to define the JavaScript objects
and properties.java.lang.IllegalAccessException
- if access is not available
to a reflected class memberjava.lang.InstantiationException
- if unable to instantiate
the named classjava.lang.reflect.InvocationTargetException
- if an exception is thrown
during execution of methods of the named classFunction
,
FunctionObject
,
READONLY
,
#defineProperty(String, Class, int)
public static <T extends Scriptable> void defineClass(Scriptable scope, java.lang.Class<T> clazz, boolean sealed) throws java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
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.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.java.lang.IllegalAccessException
- if access is not available
to a reflected class memberjava.lang.InstantiationException
- if unable to instantiate
the named classjava.lang.reflect.InvocationTargetException
- if an exception is thrown
during execution of methods of the named classpublic static <T extends Scriptable> java.lang.String defineClass(Scriptable scope, java.lang.Class<T> clazz, boolean sealed, boolean mapInheritance) throws java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
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.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.java.lang.IllegalAccessException
- if access is not available
to a reflected class memberjava.lang.InstantiationException
- if unable to instantiate
the named classjava.lang.reflect.InvocationTargetException
- if an exception is thrown
during execution of methods of the named classpublic void defineProperty(java.lang.String propertyName, java.lang.Object value, int attributes)
propertyName
- the name of the property to define.value
- the initial value of the propertyattributes
- the attributes of the JavaScript propertyScriptable.put(String, Scriptable, Object)
public static void defineProperty(Scriptable destination, java.lang.String propertyName, java.lang.Object value, int attributes)
public static void defineConstProperty(Scriptable destination, java.lang.String propertyName)
public void defineProperty(java.lang.String propertyName, java.lang.Class<?> clazz, int attributes)
The getter must be a method with zero parameters, and the setter, if found, must be a method with one parameter.
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", then
clazz
will be searched for "getFoo"
and "setFoo" methods.clazz
- the Java class to search for the getter and setterattributes
- the attributes of the JavaScript propertyScriptable.put(String, Scriptable, Object)
public void defineProperty(java.lang.String propertyName, java.lang.Object delegateTo, java.lang.reflect.Method getter, java.lang.reflect.Method setter, int attributes)
defineProperty(String,int)
.
If setter
is 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 the
delegateTo
parameter. 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);
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 propertypublic void defineOwnProperties(Context cx, ScriptableObject props)
cx
- the current Contextprops
- a map of property ids to property descriptorspublic void defineOwnProperty(Context cx, java.lang.Object id, ScriptableObject desc)
cx
- the current Contextid
- the name/index of the propertydesc
- the new property descriptor, as described in 8.6.1protected void defineOwnProperty(Context cx, java.lang.Object id, ScriptableObject desc, boolean checkValid)
cx
- the current Contextid
- the name/index of the propertydesc
- the new property descriptor, as described in 8.6.1checkValid
- whether to perform validity checksprotected void checkPropertyDefinition(ScriptableObject desc)
protected void checkPropertyChange(java.lang.String id, ScriptableObject current, ScriptableObject desc)
protected static boolean isTrue(java.lang.Object value)
protected static boolean isFalse(java.lang.Object value)
protected boolean sameValue(java.lang.Object newValue, java.lang.Object currentValue)
newValue
- the new valuecurrentValue
- the current valueprotected int applyDescriptorToAttributeBitset(int attributes, ScriptableObject desc)
protected boolean isDataDescriptor(ScriptableObject desc)
desc
- a property descriptorprotected boolean isAccessorDescriptor(ScriptableObject desc)
desc
- a property descriptorprotected boolean isGenericDescriptor(ScriptableObject desc)
desc
- a property descriptorprotected static Scriptable ensureScriptable(java.lang.Object arg)
protected static ScriptableObject ensureScriptableObject(java.lang.Object arg)
public void defineFunctionProperties(java.lang.String[] names, java.lang.Class<?> clazz, int attributes)
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.
names
- the names of the Methods to add as function propertiesclazz
- the class to search for the Methodsattributes
- the attributes of the new propertiesFunctionObject
public static Scriptable getObjectPrototype(Scriptable scope)
public static Scriptable getFunctionPrototype(Scriptable scope)
public static Scriptable getArrayPrototype(Scriptable scope)
public static Scriptable getClassPrototype(Scriptable scope, java.lang.String className)
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.scope
- an object in the scope chainclassName
- the name of the constructorpublic static Scriptable getTopLevelScope(Scriptable obj)
Walks the parent scope chain to find an object with a null parent scope (the global object).
obj
- a JavaScript objectpublic boolean isExtensible()
public void preventExtensions()
public void sealObject()
public final boolean isSealed()
sealObject()
public static java.lang.Object getProperty(Scriptable obj, java.lang.String name)
Searches the prototype chain for a property named name
.
obj
- a JavaScript objectname
- a property namename
found in
obj
or any object in its prototype chain, or
Scriptable.NOT_FOUND
if not foundpublic static <T> T getTypedProperty(Scriptable s, int index, java.lang.Class<T> 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.
s
- a JavaScript objectindex
- an integral indextype
- the required Java type of the resultname
found in
obj
or any object in its prototype chain, or
null if not found. Note that it does not return
Scriptable.NOT_FOUND
as it can ordinarily not be
converted to most of the types.public static java.lang.Object getProperty(Scriptable obj, int index)
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.
obj
- a JavaScript objectindex
- an integral indexindex
found in
obj
or any object in its prototype chain, or
Scriptable.NOT_FOUND
if not foundpublic static <T> T getTypedProperty(Scriptable s, java.lang.String name, java.lang.Class<T> type)
Searches the prototype chain for a property named name
.
s
- a JavaScript objectname
- a property nametype
- the required Java type of the resultname
found in
obj
or any object in its prototype chain, or
null if not found. Note that it does not return
Scriptable.NOT_FOUND
as it can ordinarily not be
converted to most of the types.public static boolean hasProperty(Scriptable obj, java.lang.String name)
Searches the prototype chain for a property named name
.
obj
- a JavaScript objectname
- a property namepublic static void redefineProperty(Scriptable obj, java.lang.String name, boolean isConst)
public static boolean hasProperty(Scriptable obj, int index)
Searches the prototype chain for a property with index index
.
obj
- a JavaScript objectindex
- a property indexpublic static void putProperty(Scriptable obj, java.lang.String name, java.lang.Object value)
Searches for the named property in the prototype chain. If it is found,
the value of the property in obj
is changed through a call
to Scriptable.put(String, Scriptable, Object)
on the
prototype passing obj
as the start
argument.
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 in obj
.
obj
- a JavaScript objectname
- a property namevalue
- any JavaScript value accepted by Scriptable.putpublic static void putConstProperty(Scriptable obj, java.lang.String name, java.lang.Object value)
Searches for the named property in the prototype chain. If it is found,
the value of the property in obj
is changed through a call
to Scriptable.put(String, Scriptable, Object)
on the
prototype passing obj
as the start
argument.
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 in obj
.
obj
- a JavaScript objectname
- a property namevalue
- any JavaScript value accepted by Scriptable.putpublic static void putProperty(Scriptable obj, int index, java.lang.Object value)
Searches for the indexed property in the prototype chain. If it is found,
the value of the property in obj
is changed through a call
to Scriptable.put(int, Scriptable, Object)
on the prototype
passing obj
as the start
argument. 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 in obj
.
obj
- a JavaScript objectindex
- a property indexvalue
- any JavaScript value accepted by Scriptable.putpublic static boolean deleteProperty(Scriptable obj, java.lang.String name)
Searches for a property with name
in obj or
its prototype chain. If it is found, the object's delete
method is called.
obj
- a JavaScript objectname
- a property namepublic static boolean deleteProperty(Scriptable obj, int index)
Searches for a property with index
in obj or
its prototype chain. If it is found, the object's delete
method is called.
obj
- a JavaScript objectindex
- a property indexpublic static java.lang.Object[] getPropertyIds(Scriptable obj)
obj
- a JavaScript objectpublic static java.lang.Object callMethod(Scriptable obj, java.lang.String methodName, java.lang.Object[] args)
obj
- the JavaScript objectmethodName
- the name of the function propertyargs
- the arguments for the callContext.getCurrentContext()
public static java.lang.Object callMethod(Context cx, Scriptable obj, java.lang.String methodName, java.lang.Object[] args)
cx
- the Context object associated with the current thread.obj
- the JavaScript objectmethodName
- the name of the function propertyargs
- the arguments for the callpublic final java.lang.Object getAssociatedValue(java.lang.Object key)
key
- key object to select particular value.associateValue(Object key, Object value)
public static java.lang.Object getTopScopeValue(Scriptable scope, java.lang.Object key)
getTopLevelScope(Scriptable scope)
and then searches the prototype chain of the top scope for the first
object containing the associated value with the given key.scope
- the starting scope.key
- key object to select particular value.getAssociatedValue(Object key)
public final java.lang.Object associateValue(java.lang.Object key, java.lang.Object value)
The associated values are not serialized.
key
- key object to select particular value.value
- the value to associategetAssociatedValue(Object key)
protected ScriptableObject getOwnPropertyDescriptor(Context cx, java.lang.Object id)
protected org.mozilla.javascript.ScriptableObject.Slot getSlot(Context cx, java.lang.Object id, int accessType)
public int size()
public boolean isEmpty()
public java.lang.Object get(java.lang.Object key)