Compilation of JavaScript source to class files is supported. It is possible to specify the class files as well as the packages to generate into.
JavaScript fundamental type | Java type |
Undefined | A singleton object defined by Context.getUndefinedType() |
Null | null |
Boolean | java.lang.Boolean |
Number | java.lang.Number, that is, any of java.lang.Byte, java.lang.Short, java.lang.Integer, java.lang.Float, or java.lang.Double. Not java.lang.Long, since a double representation of a long may lose precision. |
String | java.lang.String |
Object | org.mozilla.javascript.Scriptable |
In addition, ECMA refers to objects that implement [[Call]] as functions. These object types are represented by implementing the Function interface.
Since JavaScript is a dynamically typed language, the static Java type of a JavaScript value is java.lang.Object.
The behavior of the JavaScript engine is undefined if a value of any
type other than the ones described above is introduced into JavaScript.
(This caveat does not apply to scripts that use LiveConnect; the Java values
are wrapped and unwrapped as appropriate to conform to the above type constraints.)
Instead, every property accessor method in Scriptable
(has, get, set, remove, getAttributes,
and setAttributes) has overloaded forms that take either a String
or an int argument. It is the responsibility of the caller to
invoke the appropriate overloaded form. For example, evaluating the expression
obj["3"]
will invoke the get(int, Scriptable) method even though the property name
was presented in the script as a string. Similarly, values of numbers that
do not fix in integers (like 1.1 and 0x100000000) must be converted to
strings.
The easiest way to define new host objects is by using ScriptableObject.defineClass(). This method defines a set of JavaScript objects using a Java class. Several of the examples define host objects this way.
If the services provided by defineClass are insufficient, try other
methods of
ScriptableObject
and
FunctionObject,
such as defineProperty and defineFunctionProperties.