org.mozilla.javascript
Class ScriptableObject

java.lang.Object
  |
  +--org.mozilla.javascript.ScriptableObject
Direct Known Subclasses:
ImporterTopLevel, org.mozilla.javascript.NativeFunction

public abstract class ScriptableObject
extends java.lang.Object
implements Scriptable

This is the default implementation of the Scriptable interface. This class provides convenient default behavior that makes it easier to define host objects.

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:
Scriptable

Field Summary
static int DONTENUM
          Property attribute indicating property is not enumerated.
static int EMPTY
          The empty property attribute.
protected  Scriptable parent
          The parent scope of this object.
static int PERMANENT
          Property attribute indicating property cannot be deleted.
protected  Scriptable prototype
          The prototype of this object.
static int READONLY
          Property attribute indicating assignment to this property is ignored.
 
Fields inherited from interface org.mozilla.javascript.Scriptable
NOT_FOUND
 
Constructor Summary
ScriptableObject()
           
 
Method Summary
static void defineClass(Scriptable scope, java.lang.Class clazz)
          Defines JavaScript objects from a Java class.
static void defineClass(Scriptable scope, java.lang.Class clazz, boolean sealed)
          Defines JavaScript objects from a Java class, optionally allowing sealing.
 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 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.
 java.lang.Object get(int index, Scriptable start)
          Returns the value of the indexed property or NOT_FOUND.
 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.
 int getAttributes(int index, Scriptable start)
          Get the attributes of an indexed property.
 int getAttributes(java.lang.String name, Scriptable start)
          Get the attributes of a named property.
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 Scriptable getFunctionPrototype(Scriptable scope)
          Get the Function.prototype 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.
 Scriptable getParentScope()
          Returns the parent (enclosing) scope of the object.
 Scriptable getPrototype()
          Returns the prototype of the object.
static Scriptable getTopLevelScope(Scriptable obj)
          Get the global scope.
 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.
 boolean isSealed()
          Return true if this object is sealed.
 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 sealObject()
          Seal this object.
 void setAttributes(int index, Scriptable start, int attributes)
          Set the attributes of an indexed property.
 void setAttributes(java.lang.String name, Scriptable start, int attributes)
          Set the attributes of a named property.
 void setParentScope(Scriptable m)
          Sets the parent (enclosing) scope of the object.
 void setPrototype(Scriptable m)
          Sets the prototype of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY

public static final int EMPTY
The empty property attribute. Used by getAttributes() and setAttributes().
See Also:
getAttributes(java.lang.String, org.mozilla.javascript.Scriptable), setAttributes(java.lang.String, org.mozilla.javascript.Scriptable, int)

READONLY

public static final int READONLY
Property attribute indicating assignment to this property is ignored.
See Also:
put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object), getAttributes(java.lang.String, org.mozilla.javascript.Scriptable), setAttributes(java.lang.String, org.mozilla.javascript.Scriptable, int)

DONTENUM

public static final int DONTENUM
Property attribute indicating property is not enumerated. Only enumerated properties will be returned by getIds().
See Also:
getIds(), getAttributes(java.lang.String, org.mozilla.javascript.Scriptable), setAttributes(java.lang.String, org.mozilla.javascript.Scriptable, int)

PERMANENT

public static final int PERMANENT
Property attribute indicating property cannot be deleted.
See Also:
delete(java.lang.String), getAttributes(java.lang.String, org.mozilla.javascript.Scriptable), setAttributes(java.lang.String, org.mozilla.javascript.Scriptable, int)

prototype

protected Scriptable prototype
The prototype of this object.

parent

protected Scriptable parent
The parent scope of this object.
Constructor Detail

ScriptableObject

public ScriptableObject()
Method Detail

getClassName

public abstract java.lang.String 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:
getClassName in interface Scriptable

has

public boolean has(java.lang.String name,
                   Scriptable start)
Returns true if the named property is defined.
Specified by:
has in interface Scriptable
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

has

public boolean has(int index,
                   Scriptable start)
Returns true if the property index is defined.
Specified by:
has in interface Scriptable
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

get

public java.lang.Object get(java.lang.String name,
                            Scriptable start)
Returns the value of the named property or NOT_FOUND. If the property was created using defineProperty, the appropriate getter method is called.
Specified by:
get in interface Scriptable
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

get

public java.lang.Object get(int index,
                            Scriptable start)
Returns the value of the indexed property or NOT_FOUND.
Specified by:
get in interface Scriptable
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

put

public void put(java.lang.String name,
                Scriptable start,
                java.lang.Object value)
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:
put in interface Scriptable
Parameters:
name - the name of the property
start - the object whose property is being set
value - value to set the property to

put

public void put(int index,
                Scriptable start,
                java.lang.Object value)
Sets the value of the indexed property, creating it if need be.
Specified by:
put in interface Scriptable
Parameters:
index - the numeric index for the property
start - the object whose property is being set
value - value to set the property to

delete

public void delete(java.lang.String name)
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:
delete in interface Scriptable
Parameters:
name - the name of the property

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:
delete in interface Scriptable
Parameters:
index - the numeric index for the property

getAttributes

public int getAttributes(java.lang.String name,
                         Scriptable start)
                  throws PropertyException
Get the attributes of a named property. The property is specified by name as defined for has.

Parameters:
name - the identifier for the property
start - the object in which the lookup began
Returns:
the bitset of attributes
Throws:
PropertyException - if the named property is not found
See Also:
has(java.lang.String, org.mozilla.javascript.Scriptable), READONLY, DONTENUM, PERMANENT, EMPTY

getAttributes

public int getAttributes(int index,
                         Scriptable start)
                  throws PropertyException
Get the attributes of an indexed property.
Parameters:
index - the numeric index for the property
start - the object in which the lookup began
Returns:
the bitset of attributes
Throws:
PropertyException - if the indexed property is not found
See Also:
has(java.lang.String, org.mozilla.javascript.Scriptable), READONLY, DONTENUM, PERMANENT, EMPTY

setAttributes

public void setAttributes(java.lang.String name,
                          Scriptable start,
                          int attributes)
                   throws PropertyException
Set the attributes of a named property. The property is specified by 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
start - the object in which the lookup began
attributes - the bitset of attributes
Throws:
PropertyException - if the named property is not found
See Also:
Scriptable.has(java.lang.String, org.mozilla.javascript.Scriptable), READONLY, DONTENUM, PERMANENT, EMPTY

setAttributes

public void setAttributes(int index,
                          Scriptable start,
                          int attributes)
                   throws PropertyException
Set the attributes of an indexed property.
Parameters:
index - the numeric index for the property
start - the object in which the lookup began
attributes - the bitset of attributes
Throws:
PropertyException - if the indexed property is not found
See Also:
Scriptable.has(java.lang.String, org.mozilla.javascript.Scriptable), READONLY, DONTENUM, PERMANENT, EMPTY

getPrototype

public Scriptable getPrototype()
Returns the prototype of the object.
Specified by:
getPrototype in interface Scriptable
Tags copied from interface: Scriptable
Returns:
the prototype

setPrototype

public void setPrototype(Scriptable m)
Sets the prototype of the object.
Specified by:
setPrototype in interface Scriptable
Tags copied from interface: Scriptable
Parameters:
prototype - the prototype to set

getParentScope

public Scriptable getParentScope()
Returns the parent (enclosing) scope of the object.
Specified by:
getParentScope in interface Scriptable
Tags copied from interface: Scriptable
Returns:
the parent scope

setParentScope

public void setParentScope(Scriptable m)
Sets the parent (enclosing) scope of the object.
Specified by:
setParentScope in interface Scriptable
Tags copied from interface: Scriptable
Parameters:
parent - the parent scope to set

getIds

public java.lang.Object[] getIds()
Returns an array of ids for the properties of the object.

Any properties with the attribute DONTENUM are not listed.

Specified by:
getIds in interface Scriptable
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.

getAllIds

public java.lang.Object[] getAllIds()
Returns an array of ids for the properties of the object.

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.

getDefaultValue

public java.lang.Object getDefaultValue(java.lang.Class typeHint)
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 hint of null means "no hint".

Specified by:
getDefaultValue in interface Scriptable
Parameters:
typeHint - the type hint
Returns:
the default value for the object See ECMA 8.6.2.6.

hasInstance

public boolean hasInstance(Scriptable instance)
Implements the instanceof operator.

This operator has been proposed to ECMA.

Specified by:
hasInstance in interface Scriptable
Parameters:
instance - The value that appeared on the LHS of the instanceof operator
Returns:
true if "this" appears in value's prototype chain

defineClass

public static void defineClass(Scriptable scope,
                               java.lang.Class clazz)
                        throws java.lang.IllegalAccessException,
                               java.lang.InstantiationException,
                               java.lang.reflect.InvocationTargetException,
                               ClassDefinitionException,
                               PropertyException
Defines JavaScript objects from a Java class. If the given class has a method
 static void init(Scriptable scope);
then it is invoked and no further initialization is done and the result of the invocation will be returned.

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 ClassDefinitionException 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

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 ClassDefinitionException 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 ClassDefinitionException 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:
java.lang.IllegalAccessException - if access is not available to a reflected class member
java.lang.InstantiationException - if unable to instantiate the named class
java.lang.reflect.InvocationTargetException - if an exception is thrown during execution of methods of the named class
ClassDefinitionException - if an appropriate constructor cannot be found to create the prototype
PropertyException - if getter and setter methods do not conform to the requirements of the defineProperty method
See Also:
Function, FunctionObject, READONLY, defineProperty(java.lang.String, java.lang.Object, int)

defineClass

public static void defineClass(Scriptable scope,
                               java.lang.Class clazz,
                               boolean sealed)
                        throws java.lang.IllegalAccessException,
                               java.lang.InstantiationException,
                               java.lang.reflect.InvocationTargetException,
                               ClassDefinitionException,
                               PropertyException
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
sealed - whether or not to create sealed standard objects that cannot be modified.
Throws:
java.lang.IllegalAccessException - if access is not available to a reflected class member
java.lang.InstantiationException - if unable to instantiate the named class
java.lang.reflect.InvocationTargetException - if an exception is thrown during execution of methods of the named class
ClassDefinitionException - if an appropriate constructor cannot be found to create the prototype
PropertyException - if getter and setter methods do not conform to the requirements of the defineProperty method
Since:
1.4R3

defineProperty

public void defineProperty(java.lang.String propertyName,
                           java.lang.Object value,
                           int attributes)
Define a JavaScript property. Creates the property with an initial value and sets its attributes.
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.put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)

defineProperty

public void defineProperty(java.lang.String propertyName,
                           java.lang.Class clazz,
                           int attributes)
                    throws PropertyException
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", 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
Throws:
PropertyException - if multiple methods are found for the getter or setter, or if the getter or setter do not conform to the forms described in defineProperty(String, Object, Method, Method, int)
See Also:
Scriptable.put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)

defineProperty

public void defineProperty(java.lang.String propertyName,
                           java.lang.Object delegateTo,
                           java.lang.reflect.Method getter,
                           java.lang.reflect.Method setter,
                           int attributes)
                    throws PropertyException
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 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
Throws:
PropertyException - if the getter or setter do not conform to the forms specified above

defineFunctionProperties

public void defineFunctionProperties(java.lang.String[] names,
                                     java.lang.Class clazz,
                                     int attributes)
                              throws PropertyException
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:
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
Throws:
PropertyException - if any of the names has no corresponding method or more than one corresponding method in the class
See Also:
FunctionObject

getObjectPrototype

public static Scriptable getObjectPrototype(Scriptable scope)
Get the Object.prototype property. See ECMA 15.2.4.

getFunctionPrototype

public static Scriptable getFunctionPrototype(Scriptable scope)
Get the Function.prototype property. See ECMA 15.3.4.

getClassPrototype

public static Scriptable getClassPrototype(Scriptable scope,
                                           java.lang.String className)
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 chain
className - the name of the constructor
Returns:
the prototype for the named class, or null if it cannot be found.

getTopLevelScope

public static Scriptable getTopLevelScope(Scriptable obj)
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

sealObject

public void sealObject()
Seal this object. A sealed object may not have properties added or removed. Once an object is sealed it may not be unsealed.
Since:
1.4R3

isSealed

public boolean isSealed()
Return true if this object is sealed. It is an error to attempt to add or remove properties to a sealed object.
Returns:
true if sealed, false otherwise.
Since:
1.4R3