some nashorn testing

A. Sundararajan sundararajan.athijegannathan at oracle.com
Wed Jan 9 08:25:18 PST 2013


Hi Andreas,

In the line:

+ "    return Packages.ObjectArray.objFunc(firstParam, array);\n"

You convert a JS array "array" to a Java Object[]. With nashorn array 
conversion is not implicit. You need to call Java.toJavaArray convert 
script array to Java array.

+ "    return Packages.ObjectArray.objFunc(firstParam, 
Java.toJavaArray(array));\n"

Also, to make sure nashorn script engine is used, I modified engine name 
to be "nashorn". Please try the updated ObjectArray.java attached.

Thanks
-Sundar

Andreas Rieber wrote:
> I use nashorn clone from http://hg.openjdk.java.net/nashorn/jdk8/nashorn
> last update yesterday evening, and it doesn't work as standalone js 
> program as it fails scripting java so you would need to compile the 
> sample.
>
>
> On 09.01.13 16:26, Marcus Lagergren wrote:
>> Where did you get/build your nashorn.jar? We'll get proper versioning 
>> in there as soon as we've integrated fully with the OpenJDK build 
>> system. I still get nothing when I manually build the latest 
>> nashorn.jar from the openjdk and run the JS snippet from the command 
>> line with java -jar nashorn.jar <program.js>  (Haven't gone through 
>> the script engine bridge, but as it seems to be a JavaScript problem 
>> it should be sufficient to run it as a standalone script). This very 
>> much looks like a bug that I fixed before christmas and pushed into 
>> the closed repo - I've verified that it made it into the openjdk source.
>> /M
>>
>> On Jan 9, 2013, at 3:51 PM, Andreas Rieber <rieberandreas at gmail.com> 
>> wrote:
>>
>>> Hi Marcus and Remi,
>>>
>>> please find attached a better test to reproduce.
>>>
>>> Rhino output:
>>> -------------
>>> data 0 : 1.0
>>> data 1 : 2.0
>>> data 2 : 3.0
>>>
>>> Nashorn output:
>>> ---------------
>>> Exception in thread "main" java.lang.ClassCastException: Cannot cast 
>>> jdk.nashorn.internal.objects.NativeArray to [Ljava.lang.Object;
>>>     at 
>>> sun.invoke.util.ValueConversions.newClassCastException(ValueConversions.java:461) 
>>>
>>>     at 
>>> sun.invoke.util.ValueConversions.castReference(ValueConversions.java:456) 
>>>
>>>     at 
>>> jdk.nashorn.internal.scripts.Script$\^eval\_.varargFunc(<eval>:10)
>>>     at 
>>> jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:13)
>>>     at 
>>> jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:359) 
>>>
>>>     at 
>>> jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:310) 
>>>
>>>     at 
>>> jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:438) 
>>>
>>>     at 
>>> jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:423) 
>>>
>>>     at 
>>> jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150) 
>>>
>>>     at 
>>> javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
>>>     at ObjectArray.main(ObjectArray.java:16)
>>>
>>>
>>>
>>> On 09.01.13 08:45, Marcus Lagergren wrote:
>>>> Remi, Andreas
>>>>
>>>> I can't reproduce your problem running
>>>>
>>>> java -jar nashorn.jar script.js
>>>>
>>>> where script.js is
>>>>
>>>> -- 
>>>>   var array;
>>>>   try {
>>>>     array = new (Java.type("java.lang.Object[]"))(len);
>>>>   } catch (x) {
>>>>     array = [];
>>>>   }
>>>> -- 
>>>>
>>>> Do you have a reproducer you can send me ? I know there was a bug 
>>>> regarding array literal types just before Christmas, but it's in 
>>>> the openjdk as far as I can tell.
>>>>
>>>> Regards
>>>> Marcus
>>>>
>>>>
>>>> On Jan 8, 2013, at 10:56 PM, Remi Forax <forax at univ-mlv.fr> wrote:
>>>>
>>>>> On 01/08/2013 10:49 PM, Attila Szegedi wrote:
>>>>>> Hey Marcus and Jim, this is similar to that bug we saw before 
>>>>>> with array literals in the ternary operator. Something related to 
>>>>>> codegen's inference of types…
>>>>> a simple workaround is to change the last line
>>>>>
>>>>>   var array;
>>>>>   try {
>>>>>     array = new (Java.type("java.lang.Object[]"))(len);  // array 
>>>>> is Object[]
>>>>>   } catch (x) {
>>>>>     //array = [];                                        // array 
>>>>> is NativeArray
>>>>>     array = new (Java.type("java.lang.Object[]"))(0);    // array 
>>>>> is Object[]
>>>>>   }
>>>>>
>>>>> Rémi
>>>>>
>>>>>> On Jan 8, 2013, at 9:30 PM, Andreas Rieber 
>>>>>> <rieberandreas at gmail.com> wrote:
>>>>>>
>>>>>>> I tried some cases and will go for (Nashorn and Rhino):
>>>>>>>
>>>>>>>    var array;
>>>>>>>    try {
>>>>>>>      array = new (Java.type("java.lang.Object[]"))(len);
>>>>>>>    } catch (x) {
>>>>>>>      array = [];
>>>>>>>    }
>>>>>>>
>>>>>>> The catch block works with Rhino but gives with Nashorn:
>>>>>>>
>>>>>>> Exception in thread "main" java.lang.ClassCastException: Cannot 
>>>>>>> cast jdk.nashorn.internal.objects.NativeArray to 
>>>>>>> [Ljava.lang.Object;
>>>>>>>      at 
>>>>>>> sun.invoke.util.ValueConversions.newClassCastException(ValueConversions.java:461) 
>>>>>>>
>>>>>>>      at 
>>>>>>> sun.invoke.util.ValueConversions.castReference(ValueConversions.java:456) 
>>>>>>>
>>>>>>>      at 
>>>>>>> jdk.nashorn.internal.scripts.Script$main.sprintf(main.js:114)
>>>>>>>      at 
>>>>>>> jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:350) 
>>>>>>>
>>>>>>>      at 
>>>>>>> jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:310) 
>>>>>>>
>>>>>>>      at 
>>>>>>> jdk.nashorn.internal.objects.NativeFunction.apply(NativeFunction.java:133) 
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> cheers
>>>>>>> Andreas
>>>>>>>
>>> <ObjectArray.java>
>

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ObjectArray.java
Url: http://mail.openjdk.java.net/pipermail/nashorn-dev/attachments/20130109/b17f3dc7/ObjectArray.java 


More information about the nashorn-dev mailing list