org.mozilla.javascript
public abstract class ScriptableObject extends Object implements Scriptable, Serializable, DebuggableObject
Various properties and methods of JavaScript objects can be conveniently defined using methods of ScriptableObject.
Classes extending ScriptableObject must define the getClassName method.
See Also: Scriptable
Field Summary | |
---|---|
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.
|
Constructor Summary | |
---|---|
ScriptableObject() | |
ScriptableObject(Scriptable scope, Scriptable prototype) |
Method Summary | |
---|---|
Object | associateValue(Object key, Object value)
Associate arbitrary application-specific value with this object.
|
static Object | callMethod(Scriptable obj, String methodName, Object[] args)
Call a method of an object. |
static Object | callMethod(Context cx, Scriptable obj, String methodName, Object[] args)
Call a method of an object. |
static void | defineClass(Scriptable scope, Class clazz)
Defines JavaScript objects from a Java class that implements Scriptable.
|
static void | defineClass(Scriptable scope, Class clazz, boolean sealed)
Defines JavaScript objects from a Java class, optionally
allowing sealing.
|
static String | defineClass(Scriptable scope, Class 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 | defineFunctionProperties(String[] names, Class clazz, int attributes)
Search for names in a class, adding the resulting methods
as properties.
|
void | defineProperty(String propertyName, Object value, int attributes)
Define a JavaScript property.
|
static void | defineProperty(Scriptable destination, String propertyName, Object value, int attributes)
Utility method to add properties to arbitrary Scriptable object.
|
void | defineProperty(String propertyName, Class clazz, int attributes)
Define a JavaScript property with getter and setter side effects.
|
void | defineProperty(String propertyName, Object delegateTo, Method getter, Method setter, int attributes)
Define a JavaScript property.
|
void | delete(String name)
Removes a named property from the object.
|
void | delete(int index)
Removes the indexed property from the object.
|
static boolean | deleteProperty(Scriptable obj, String name)
Removes the property from an object or its prototype chain.
|
static boolean | deleteProperty(Scriptable obj, int index)
Removes the property from an object or its prototype chain.
|
protected Object | equivalentValues(Object value)
Custom == operator.
|
Object | get(String name, Scriptable start)
Returns the value of the named property or NOT_FOUND.
|
Object | get(int index, Scriptable start)
Returns the value of the indexed property or NOT_FOUND.
|
Object[] | getAllIds()
Returns an array of ids for the properties of the object.
|
Object | getAssociatedValue(Object key)
Get arbitrary application-specific value associated with this object. |
int | getAttributes(String name, Scriptable start) |
int | getAttributes(int index, Scriptable start) |
int | getAttributes(String name)
Get the attributes of a named property.
|
int | getAttributes(int index)
Get the attributes of an indexed property.
|
abstract String | getClassName()
Return the name of the class.
|
static Scriptable | getClassPrototype(Scriptable scope, String className)
Get the prototype for the named class.
|
Object | getDefaultValue(Class typeHint)
Implements the [[DefaultValue]] internal method.
|
static Scriptable | getFunctionPrototype(Scriptable scope)
Get the Function.prototype property.
|
Object[] | getIds()
Returns an array of ids for the properties of the object.
|
static Scriptable | getObjectPrototype(Scriptable scope)
Get the Object.prototype property.
|
Scriptable | getParentScope()
Returns the parent (enclosing) scope of the object. |
static Object | getProperty(Scriptable obj, String name)
Gets a named property from an object or any object in its prototype chain.
|
static Object | getProperty(Scriptable obj, int index)
Gets an indexed property from an object or any object in its prototype chain.
|
static Object[] | getPropertyIds(Scriptable obj)
Returns an array of all ids from an object and its prototypes.
|
Scriptable | getPrototype()
Returns the prototype of the object. |
static Scriptable | getTopLevelScope(Scriptable obj)
Get the global scope.
|
static Object | getTopScopeValue(Scriptable scope, Object key)
Get arbitrary application-specific value associated with the top scope
of the given scope.
|
boolean | has(String name, Scriptable start)
Returns true if the named property is defined.
|
boolean | has(int index, Scriptable start)
Returns true if the property index is defined.
|
boolean | hasInstance(Scriptable instance)
Implements the instanceof operator.
|
static boolean | hasProperty(Scriptable obj, String name)
Returns whether a named property is defined in an object or any object
in its prototype chain.
|
static boolean | hasProperty(Scriptable obj, int index)
Returns whether an indexed property is defined in an object or any object
in its prototype chain.
|
boolean | isSealed()
Return true if this object is sealed.
|
void | put(String name, Scriptable start, Object value)
Sets the value of the named property, creating it if need be.
|
void | put(int index, Scriptable start, Object value)
Sets the value of the indexed property, creating it if need be.
|
static void | putProperty(Scriptable obj, String name, Object value)
Puts a named property in an object or in an object in its prototype chain.
|
static void | putProperty(Scriptable obj, int index, Object value)
Puts an indexed property in an object or in an object in its prototype chain.
|
void | sealObject()
Seal this object.
|
void | setAttributes(String name, Scriptable start, int attributes) |
void | setAttributes(int index, Scriptable start, int attributes) |
void | setAttributes(String name, int attributes)
Set the attributes of a named property.
|
void | setAttributes(int index, int attributes)
Set the attributes of an indexed property.
|
void | setParentScope(Scriptable m)
Sets the parent (enclosing) scope of the object. |
void | setPrototype(Scriptable m)
Sets the prototype of the object. |
See Also: ScriptableObject ScriptableObject ScriptableObject
See Also: ScriptableObject ScriptableObject
See Also: ScriptableObject ScriptableObject ScriptableObject
See Also: ScriptableObject ScriptableObject ScriptableObject
The associated values are not serilized.
Parameters: key key object to select particular value. value the value to associate
Returns: the passed value if the method is called first time for the given key or old value for any subsequent calls.
See Also: ScriptableObject
Parameters: obj the JavaScript object methodName the name of the function property args the arguments for the call
See Also: getCurrentContext
Parameters: cx the Context object associated with the current thread. obj the JavaScript object methodName the name of the function property args the arguments for the call
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. 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.
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 member InstantiationException if unable to instantiate the named class InvocationTargetException if an exception is thrown during execution of methods of the named class
See Also: Function FunctionObject READONLY ScriptableObject
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 member InstantiationException if unable to instantiate the named class InvocationTargetException if an exception is thrown during execution of methods of the named class
Since: 1.4R3
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 member InstantiationException if unable to instantiate the named class InvocationTargetException if an exception is thrown during execution of methods of the named class
Since: 1.6R2
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: names the names of the Methods to add as function properties clazz the class to search for the Methods attributes the attributes of the new properties
See Also: FunctionObject
Parameters: propertyName the name of the property to define. value the initial value of the property attributes the attributes of the JavaScript property
See Also: Scriptable
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", then
clazz
will be searched for "getFoo"
and "setFoo" methods. clazz the Java class to search for the getter and setter attributes the attributes of the JavaScript property
See Also: Scriptable
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(ScriptableObject obj); static void setFoo(ScriptableObject 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(ScriptableObject obj); void setFoo(ScriptableObject 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 property setter the method to invoke to set the value of the property attributes the attributes of the JavaScript property
Parameters: name the name of the property
Parameters: index the numeric index for the property
Searches for a property with name
in obj or
its prototype chain. If it is found, the object's delete
method is called.
Parameters: obj a JavaScript object name a property name
Returns: true if the property doesn't exist or was successfully removed
Since: 1.5R2
Searches for a property with index
in obj or
its prototype chain. If it is found, the object's delete
method is called.
Parameters: obj a JavaScript object index a property index
Returns: true if the property doesn't exist or was successfully removed
Since: 1.5R2
The default implementation returns Boolean.TRUE if this == value or NOT_FOUND otherwise. It indicates that by default custom equality is available only if value is this in which case true is returned.
Parameters: name the name of the property start the object in which the lookup began
Returns: the value of the property (may be null), or NOT_FOUND
Parameters: index the numeric index for the property start the object in which the lookup began
Returns: the value of the property (may be null), or NOT_FOUND
All properties, even those with attribute DONTENUM, are listed.
Returns: an array of java.lang.Objects with an entry for every listed 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.
Parameters: key key object to select particular value.
See Also: ScriptableObject
Deprecated: Use ScriptableObject. The engine always ignored the start argument.
Deprecated: Use ScriptableObject. The engine always ignored the start argument.
name
as defined for has
.
Parameters: name the identifier for the property
Returns: the bitset of attributes
Throws: EvaluatorException if the named property is not found
See Also: ScriptableObject READONLY DONTENUM PERMANENT EMPTY
Parameters: index the numeric index for the property
Returns: the bitset of attributes
Throws: EvaluatorException if the named property is not found is not found
See Also: ScriptableObject READONLY DONTENUM PERMANENT EMPTY
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 chain className the name of the constructor
Returns: the prototype for the named class, or null if it cannot be found.
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".
Parameters: typeHint the type hint
Returns: the default value for the object See ECMA 8.6.2.6.
Any properties with the attribute DONTENUM are not listed.
Returns: an array of java.lang.Objects with an entry for every listed 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.
Searches the prototype chain for a property named name
.
Parameters: obj a JavaScript object name a property name
Returns: the value of a property with name name
found in
obj
or any object in its prototype chain, or
Scriptable.NOT_FOUND
if not found
Since: 1.5R2
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 object index an integral index
Returns: the value of a property with index index
found in
obj
or any object in its prototype chain, or
Scriptable.NOT_FOUND
if not found
Since: 1.5R2
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
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
Parameters: scope the starting scope. key key object to select particular value.
See Also: ScriptableObject
Parameters: name the name of the property start the object in which the lookup began
Returns: true if and only if the property was found in the object
Parameters: index the numeric index for the property start the object in which the lookup began
Returns: true if and only if the property was found in the object
This operator has been proposed to ECMA.
Parameters: instance The value that appeared on the LHS of the instanceof operator
Returns: true if "this" appears in value's prototype chain
Searches the prototype chain for a property named name
.
Parameters: obj a JavaScript object name a property name
Returns: the true if property was found
Since: 1.5R2
Searches the prototype chain for a property with index index
.
Parameters: obj a JavaScript object index a property index
Returns: the true if property was found
Since: 1.5R2
Returns: true if sealed, false otherwise.
Since: 1.4R3
If the property's attributes include READONLY, no action is taken. This method will actually set the property in the start object.
Parameters: name the name of the property start the object whose property is being set value value to set the property to
Parameters: index the numeric index for the property start the object whose property is being set value value to set the property to
Seaches 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 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
.
Parameters: obj a JavaScript object name a property name value any JavaScript value accepted by Scriptable.put
Since: 1.5R2
Seaches 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 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
.
Parameters: obj a JavaScript object index a property index value any JavaScript value accepted by Scriptable.put
Since: 1.5R2
Since: 1.4R3
Deprecated: Use ScriptableObject. The engine always ignored the start argument.
Deprecated: Use ScriptableObject. The engine always ignored the start argument.
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.
Parameters: name the name of the property attributes the bitset of attributes
Throws: EvaluatorException if the named property is not found
See Also: Scriptable READONLY DONTENUM PERMANENT EMPTY
Parameters: index the numeric index for the property attributes the bitset of attributes
Throws: EvaluatorException if the named property is not found
See Also: Scriptable READONLY DONTENUM PERMANENT EMPTY