CosmoCode
  • Great software.

  • Bright people.

  • Happy customers!

CosmoCode GmbH
  • Home
  • Skills
  • About Us
  • References
  • Blog
  • Open Source
←
All blogposts
→

jsr223/javax.script experiences and problems

These days I tried to replace the (almost exciting but mysterious) rhino by the uprising javax.script technology which would be included in the next big Java release (Mustang aka jdk1.6, available in ?late october 2006?). I am using rhino for 2 years and I am steadily excited about it. I love java's capabilities (you may call it complexity), and I love javascript's simplicity. With rhino, they link together not only two technologies, but also different users: backend-framework-hardcore-java-app…

Detlef Hüttemann, 06/20/2006 4:15 p.m.

jsr223/javax.script experiences and problems

These days I tried to replace the (almost exciting but mysterious) rhino by the uprising javax.script technology which would be included in the next big Java release (Mustang aka jdk1.6, available in ?late october 2006?).

I am using rhino for 2 years and I am steadily excited about it. I love java's capabilities (you may call it complexity), and I love javascript's simplicity. With rhino, they link together not only two technologies, but also different users: backend-framework-hardcore-java-application-developers on one side, lightweight-changeable-customizing-script-coders on the other side. This works great on our CMS, and I wont`change it - up to now.

With jsr223, the java community process decided to go one step beyond. Jsr223 describes an abstract API for the integration arbitrary script engines. Wow - that sounds great! You are free to choose the script engine - if you hate rhino( aka EcmaScript aka JavaScript), use groovy instead. Or any other script language available.

So I went out and redesigned our application:

  • At a first glance, the API is very well designed. It clear, simple, straightforward.
  • I first tried to run the reference implementation of the jsr223. But I think thats an older API design; its better to use the Mustang SE instead - the API`s differs (Namespace was renamed to Bindings for example).
  • Since the javax.script adds another abstraction layer, I expect further performance drawbacks.

OK - I redesigned the app it kept it running!

But quickly I stumbled into a problem. The follwing javascript wont work anymore:

  • *test.js: var foo = null; if ( foo == null ) foo = bar; test.java: import javax.script.*; import java.io.*; public class test { public static void main( String [] args ) throws Exception { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine se = mgr.getEngineByName( “rhino” ) ; se.eval( new FileReader( args[0] )); } }; huettemann@devel: > java -cp . test test.js**

Exception in thread “main” javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: “foo” is not defined. (#3) in at line number 3

        at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:93)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:230)
        at test.main(test.java:11)

Thats crucial, because in java you often have to deal with null values and you need a convenient way to handle them in javascript. Note: The example above works fine with plain rhino!

The following workaround is not very suprising:

if ( typeof(foo) == ' undefined' ) 
  ...

The other point which isn`t suprising is what read in Sundararajan blog:

javascript in javax.script won`t support bytecode mode - it only runs in interpreted mode.

At this time I cannot estimate the effects, but it does not sound joyful: I assume, that the interpreted mode wont run as fast as the compiled/bytecode mode. With these two disadvantages I will continue the javax.script integration only if they are solved.

If anyone had solved the null problem - it would be great if you can send me the solution!

Read more

  • Collections.sort in Rhino
  • rhino again
  • The return of flat formel
  • Publizieren in Rhino
  • Java, a developer's island without trees
  • Rhino Javascript Array secrets
  • Schwedenurlaub mit CosmoCode
  • autounboxing NullPointerException
  • Palava 1.0 released
  • Uncaught exceptions in scheduled tasks

Contact

Thank you for your interest!
Get in contact:

CosmoCode GmbH

Prenzlauer Allee 36G
10405 Berlin

Phone: +49 30 814 50 40 70

Fax: +49 30 2809 7093


mail: info@cosmocode.de

CosmoCode GmbH  
   

© CosmoCode 2021 | Imprint | Data Privacy | Cookies verwalten

Close
Deutsch English
  • Jobs