org.mozilla.javascript

Class IdScriptableObject

Implemented Interfaces:
DebuggableObject, IdFunctionCall, Scriptable, Serializable
Known Direct Subclasses:
BaseFunction, Continuation, ImporterTopLevel, NativeArray, NativeCall, NativeObject, NativeRegExp, XMLObject

public abstract class IdScriptableObject
extends ScriptableObject
implements IdFunctionCall

Base class for native object implementation that uses IdFunctionObject to export its methods to script via .prototype object. Any descendant should implement at least the following methods: findInstanceIdInfo getInstanceIdName execIdCall methodArity To define non-function properties, the descendant should override getInstanceIdValue setInstanceIdValue to get/set property value and provide its default attributes. To customize initializition of constructor and protype objects, descendant may override scopeInit or fillConstructorProperties methods.

Field Summary

Fields inherited from class org.mozilla.javascript.ScriptableObject

DONTENUM, EMPTY, PERMANENT, READONLY

Fields inherited from interface org.mozilla.javascript.Scriptable

NOT_FOUND

Constructor Summary

IdScriptableObject()
IdScriptableObject(Scriptable scope, Scriptable prototype)

Method Summary

void
activatePrototypeMap(int maxPrototypeId)
protected void
addIdFunctionProperty(Scriptable obj, Object tag, int id, String name, int arity)
protected Object
defaultGet(String name)
protected void
defaultPut(String name, Object value)
void
delete(String name)
Removes a property from this object.
Object
execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
'thisObj' will be null if invoked as constructor, in which case instance of Scriptable should be returned.
IdFunctionObject
exportAsJSClass(int maxPrototypeId, Scriptable scope, boolean sealed)
protected void
fillConstructorProperties(IdFunctionObject ctor)
protected int
findInstanceIdInfo(String name)
Map name to id of instance property.
protected int
findPrototypeId(String name)
Object
get(String name, Scriptable start)
Get a named property from the object.
int
getAttributes(String name)
protected String
getInstanceIdName(int id)
Map id back to property name it defines.
protected Object
getInstanceIdValue(int id)
Get id value.
protected int
getMaxInstanceId()
Get maximum id findInstanceIdInfo can generate.
boolean
has(String name, Scriptable start)
Indicates whether or not a named property is defined in an object.
boolean
hasPrototypeMap()
protected static EcmaError
incompatibleCallError(IdFunctionObject f)
Utility method to construct type error to indicate incompatible call when converting script thisObj to a particular type is not possible.
void
initPrototypeConstructor(IdFunctionObject f)
protected void
initPrototypeId(int id)
void
initPrototypeMethod(Object tag, int id, String name, int arity)
void
initPrototypeValue(int id, String name, Object value, int attributes)
protected static int
instanceIdInfo(int attributes, int id)
void
put(String name, Scriptable start, Object value)
Sets a named property in this object.
void
setAttributes(String name, int attributes)
protected void
setInstanceIdValue(int id, Object value)
Set or delete id value.

Methods inherited from class org.mozilla.javascript.ScriptableObject

associateValue, callMethod, callMethod, defineClass, defineClass, defineClass, defineFunctionProperties, defineProperty, defineProperty, defineProperty, defineProperty, delete, delete, deleteProperty, deleteProperty, equivalentValues, get, get, getAllIds, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getClassName, getClassPrototype, getDefaultValue, getFunctionPrototype, getIds, getObjectPrototype, getParentScope, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, getTopScopeValue, has, has, hasInstance, hasProperty, hasProperty, isSealed, put, put, putProperty, putProperty, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setParentScope, setPrototype

Constructor Details

IdScriptableObject

public IdScriptableObject()

IdScriptableObject

public IdScriptableObject(Scriptable scope,
                          Scriptable prototype)

Method Details

activatePrototypeMap

public final void activatePrototypeMap(int maxPrototypeId)

addIdFunctionProperty

protected void addIdFunctionProperty(Scriptable obj,
                                     Object tag,
                                     int id,
                                     String name,
                                     int arity)

defaultGet

protected final Object defaultGet(String name)

defaultPut

protected final void defaultPut(String name,
                                Object value)

delete

public void delete(String name)
Removes a property from this object. This operation corresponds to the ECMA [[Delete]] except that the no result is returned. The runtime will guarantee that this method is called only if the property exists. After this method is called, the runtime will call Scriptable.has to see if the property has been removed in order to determine the boolean result of the delete operator as defined by ECMA 11.4.1.

A property can be made permanent by ignoring calls to remove it.

The property is specified by a String name as defined for get.

To delete properties defined in a prototype chain, see deleteProperty in ScriptableObject.

Specified by:
delete in interface Scriptable
Overrides:
delete in interface ScriptableObject
Parameters:
name - the identifier for the property
See Also:
org.mozilla.javascript.Scriptable.get, org.mozilla.javascript.ScriptableObject.deleteProperty

execIdCall

public Object execIdCall(IdFunctionObject f,
                         Context cx,
                         Scriptable scope,
                         Scriptable thisObj,
                         Object[] args)
'thisObj' will be null if invoked as constructor, in which case instance of Scriptable should be returned.
Specified by:
execIdCall in interface IdFunctionCall

exportAsJSClass

public final IdFunctionObject exportAsJSClass(int maxPrototypeId,
                                              Scriptable scope,
                                              boolean sealed)

fillConstructorProperties

protected void fillConstructorProperties(IdFunctionObject ctor)

findInstanceIdInfo

protected int findInstanceIdInfo(String name)

findPrototypeId

protected int findPrototypeId(String name)

get

public Object get(String name,
                  Scriptable start)
Get a named property from the object. Looks property up in this object and returns the associated value if found. Returns NOT_FOUND if not found. Note that this method is not expected to traverse the prototype chain. This is different from the ECMA [[Get]] operation. Depending on the property selector, the runtime will call this method or the form of get that takes an integer:
JavaScript codeJava code
a.b a.get("b", a)
a["foo"] a.get("foo", a)
a[3] a.get(3, a)
a["3"] a.get(3, a)
a[3.0] a.get(3, a)
a["3.0"] a.get("3.0", a)
a[1.1] a.get("1.1", a)
a[-4] a.get(-4, a)

The values that may be returned are limited to the following:

  • java.lang.Boolean objects
  • java.lang.String objects
  • java.lang.Number objects
  • org.mozilla.javascript.Scriptable objects
  • null
  • The value returned by Context.getUndefinedValue()
  • NOT_FOUND
Specified by:
get in interface Scriptable
Overrides:
get in interface ScriptableObject
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

getAttributes

public int getAttributes(String name)
Overrides:
getAttributes in interface ScriptableObject

getInstanceIdName

protected String getInstanceIdName(int id)
Map id back to property name it defines.

getInstanceIdValue

protected Object getInstanceIdValue(int id)
Get id value. If id value is constant, descendant can call cacheIdValue to store value in the permanent cache. Default implementation creates IdFunctionObject instance for given id and cache its value

getMaxInstanceId

protected int getMaxInstanceId()
Get maximum id findInstanceIdInfo can generate.

has

public boolean has(String name,
                   Scriptable start)
Indicates whether or not a named property is defined in an object. Does not traverse the prototype chain.

The property is specified by a String name as defined for the get method.

Specified by:
has in interface Scriptable
Overrides:
has in interface ScriptableObject
Parameters:
name - the name of the property
start - the object in which the lookup began
Returns:
true if and only if the named property is found in the object
See Also:
org.mozilla.javascript.Scriptable.get, org.mozilla.javascript.ScriptableObject.getProperty

hasPrototypeMap

public final boolean hasPrototypeMap()

incompatibleCallError

protected static EcmaError incompatibleCallError(IdFunctionObject f)
Utility method to construct type error to indicate incompatible call when converting script thisObj to a particular type is not possible. Possible usage would be to have a private function like realThis:
  private static NativeSomething realThis(Scriptable thisObj,
                                          IdFunctionObject f)
  {
      if (!(thisObj instanceof NativeSomething))
          throw incompatibleCallError(f);
      return (NativeSomething)thisObj;
 }
 
Note that although such function can be implemented universally via java.lang.Class.isInstance(), it would be much more slower.
Parameters:
Returns:
Scriptable object suitable for a check by the instanceof operator.

initPrototypeConstructor

public final void initPrototypeConstructor(IdFunctionObject f)

initPrototypeId

protected void initPrototypeId(int id)

initPrototypeMethod

public final void initPrototypeMethod(Object tag,
                                      int id,
                                      String name,
                                      int arity)

initPrototypeValue

public final void initPrototypeValue(int id,
                                     String name,
                                     Object value,
                                     int attributes)

instanceIdInfo

protected static int instanceIdInfo(int attributes,
                                    int id)

put

public void put(String name,
                Scriptable start,
                Object value)
Sets a named property in this object.

The property is specified by a string name as defined for get.

The possible values that may be passed in are as defined for get. A class that implements this method may choose to ignore calls to set certain properties, in which case those properties are effectively read-only.

For properties defined in a prototype chain, use putProperty in ScriptableObject.

Note that if a property a is defined in the prototype p of an object o, then evaluating o.a = 23 will cause set to be called on the prototype p with o as the start parameter. To preserve JavaScript semantics, it is the Scriptable object's responsibility to modify o.

This design allows properties to be defined in prototypes and implemented in terms of getters and setters of Java values without consuming slots in each instance.

The values that may be set are limited to the following:

  • java.lang.Boolean objects
  • java.lang.String objects
  • java.lang.Number objects
  • org.mozilla.javascript.Scriptable objects
  • null
  • The value returned by Context.getUndefinedValue()

Arbitrary Java objects may be wrapped in a Scriptable by first calling Context.toObject. This allows the property of a JavaScript object to contain an arbitrary Java object as a value.

Note that has will be called by the runtime first before set is called to determine in which object the property is defined. Note that this method is not expected to traverse the prototype chain, which is different from the ECMA [[Put]] operation.

Specified by:
put in interface Scriptable
Overrides:
put in interface ScriptableObject
Parameters:
name - the name of the property
start - the object whose property is being set
value - value to set the property to
See Also:
org.mozilla.javascript.Scriptable.has, org.mozilla.javascript.Scriptable.get, org.mozilla.javascript.ScriptableObject.putProperty, org.mozilla.javascript.Context.toObject

setAttributes

public void setAttributes(String name,
                          int attributes)
Overrides:
setAttributes in interface ScriptableObject

setInstanceIdValue

protected void setInstanceIdValue(int id,
                                  Object value)
Set or delete id value. If value == NOT_FOUND , the implementation should make sure that the following getInstanceIdValue return NOT_FOUND.