Q. How do I create a Java array from JavaScript?
A. You must use Java reflection. For instance, to create an array of java.lang.String of length five, do
var stringArray = java.lang.reflect.Array.newInstance(java.lang.String, 5);Then if you wish to assign the string "hi" to the first element, simply execute stringArray[0] = "hi".
Creating arrays of primitive types is slightly different: you must use the TYPE field. For example, creating an array of seven ints can be done with the code
var intArray = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, 7);
Q. When I try to execute a script
I get the exception Required security context missing.
What's going on?
A. You've likely missed placing the Security.properties file in your class path at org.mozilla.javascript.resources.
Q. Is it possible to make Rhino classes serializable?
A. A number of people have asked about making Rhino data serializable. It certainly seems like it would be useful for a variety of applications.
The reason I haven't implemented Serializiable has been that java.lang.Class is not Serializable, so any functions or scripts that have been compiled to Java bytecodes can't be serialized. One Rhino embedder suggested that we could get around this restriction by actually saving the bytecode in a byte array and then having some mechanism for loading those bytes at the destination. That would work, but is an extra mechanism beyond standard serialization and imposes a significant space overhead on all users of compiled scripts. I'm also curious as to whether that mechanism would be any faster than simply recompiling from the script source, which would be significantly smaller than the serialized form.
It'd be great to have some solutions to the serialization problem, and I'd certainly be happy to roll changes back into Rhino.
Perhaps the best solution is to implement some serialization for Rhino's
interpretive mode and assume for Rhino's compiled mode that the class has
also been compiled on the receiving end (which is the assumption Java makes).