some nashorn testing
Jim Laskey (Oracle)
james.laskey at oracle.com
Tue Jan 8 10:12:56 PST 2013
On 2013-01-08, at 1:22 PM, Andreas Rieber <rieberandreas at gmail.com> wrote:
> Hi,
>
> i tested Nashorn with a project i am currently working on (scripting
> java). The first thing i spotted is that Rhino comes with print() and
> println() as default functions in:
>
> jdk8/jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java
>
> while Nashorn has only a print():
>
> jdk8/nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
> jdk8/nashorn/src/jdk/nashorn/api/scripting/resources/engine.js
>
> Will this be changed or is that a migration issue?
We opted for print because we were primarily using V8 performance tests and v8 only supports print. Standalone rhino does not support println either. We did have a switch for turning on println, but it fell out of favour to be consistent with other VMs.
You can duplicate the behaviour with;
function print(s) { java.lang.System.out.print("" + s); }
function println(s) { java.lang.System.out.println("" + s); }
>
> importClass works only with: load("nashorn:mozilla_compat.js");
> What is with importPackage (also used in jrunscript)?
>
importClass is being phased out since it duplicates the builtin behaviour of Packages and JavaClass.
importPackage(java.awt);
importClass(java.awt.Frame);
is just
var awt = java.awt;
var Frame = java.awt.Frame;
> Next problem i had was related to java array creation. Here the sample
> from JSE 7 Documentation "Java Scripting Programmer's Guide":
>
> // create Java String array of 5 elements
> var a = java.lang.reflect.Array.newInstance(java.lang.String, 5);
>
> // Accessing elements and length access is by usual Java syntax
> a[0] = "scripting is great!";
> print(a.length);
>
> It works with Rhino but fails with Nashorn:
>
> Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: None of the fixed arity signatures [(java.lang.Class, int[]), (java.lang.Class, int)] or the variable arity signatures [(java.lang.Class, int...)] of the method java.lang.reflect.Array.newInstance match the argument types [org.dynalang.dynalink.beans.StaticClass, java.lang.Integer]
>
> The default number type for Rhino in vararg functions is double while
> Nashorn has also integer (nice), might be a migration issue.
We are pushing the Java syntax here to eliminate confusion between the java class and the JavaClass javascript object. This eliminates the confusion when accessing members of the (String) class when a javascript object and members of the class itself when a java object.
jjs> java.lang.String
[JavaClass java.lang.String]
jjs> java.lang.String.class
class java.lang.String
so
var a = java.lang.reflect.Array.newInstance(java.lang.String.class, 5);
>
> Is jrunscript and the scriptpad sample application
> (jdk8/jdk/src/share/sample/scripting/scriptpad) now also developed
> under Nashorn project? I would like to contribute a patch for
> jrunscript (printf never worked and i added also a sprintf), the
> scriptpad needed only a few fixes to work with Rhino. I signed the OCA
> recently.
Contributions welcome.
I just see Attila answered differently, but both forms work.
>
> best regards
> Andreas
>
More information about the nashorn-dev
mailing list