Graaljs: Is shell scripting mode supported?

Created on 16 Jun 2020  路  3Comments  路  Source: oracle/graaljs

Hello,
Currently I'm using a couple of scripts on Linux that utilize the shell scripting capability in Nashorn so I'm able to just run the script from the command line.

I'm thinking about this mode:
https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html

The script has a shebang that points to jjs, e.g. #!/usr/bin/jjs -fv --language=es6.

Does graaljs support something similar?

I did a simple test where I edited the shebang to #!/root/graalvm-ce-java11-20.1.0/bin/js --experimental-options --js.nashorn-compat=true and the I tried running my script: ./myscript.js debug, I get the following:

ERROR: Unrecognized argument: '--experimental-options --js.nashorn-compat=true'. Use --help for usage instructions.

Maybe that's the way it's supposed to be?

Thanks!

Most helpful comment

Hi @idsecurity, thanks for experimenting with Graal.js.

On many systems, shebang does not split the arguments passed to the executable.
In your example, the js launcher receives a single argument ('--experimental-options --js.nashorn-compat=true') rather than two ('--experimental-options, --js.nashorn-compat=true'), and therefore fails to parse them.

Recent versions of env introduced a -S argument to split the arguments:

-S, --split-string=S  process and split S into separate arguments;
                        used to pass multiple arguments on shebang lines

In your case, you would have to add $GRAALVM_HOME/bin to $PATH and have this shebang:

#!/usr/bin/env -S js --experimental-options --js.nashorn-compat=true

If you run with an older env, I suggest you to write a proxy script named, for example, /root/graalvm-ce-java11-20.1.0/bin/graaljs-nashorn that passes the arguments that enable Nashorn compatibility to the js launcher:

#!/bin/bash

exec /root/graalvm-ce-java11-20.1.0/bin/js --experimental-options --js.nashorn-compat=true $*

In myscript.js you could then have:

#!/usr/bin/env graaljs-nashorn

There are probably more elegant solutions, but this should work around the issue.

All 3 comments

Hi @idsecurity, thanks for experimenting with Graal.js.

On many systems, shebang does not split the arguments passed to the executable.
In your example, the js launcher receives a single argument ('--experimental-options --js.nashorn-compat=true') rather than two ('--experimental-options, --js.nashorn-compat=true'), and therefore fails to parse them.

Recent versions of env introduced a -S argument to split the arguments:

-S, --split-string=S  process and split S into separate arguments;
                        used to pass multiple arguments on shebang lines

In your case, you would have to add $GRAALVM_HOME/bin to $PATH and have this shebang:

#!/usr/bin/env -S js --experimental-options --js.nashorn-compat=true

If you run with an older env, I suggest you to write a proxy script named, for example, /root/graalvm-ce-java11-20.1.0/bin/graaljs-nashorn that passes the arguments that enable Nashorn compatibility to the js launcher:

#!/bin/bash

exec /root/graalvm-ce-java11-20.1.0/bin/js --experimental-options --js.nashorn-compat=true $*

In myscript.js you could then have:

#!/usr/bin/env graaljs-nashorn

There are probably more elegant solutions, but this should work around the issue.

Hi @idsecurity

thanks for your question.

@ansalond just answered the question around the multiple arguments - that is a problem with your hashbang syntax, not with GraalVM.

For your question itself: we have a --js.scripting option that is simulating the scripting mode also available in Nashorn/JJS.

Finally, you are passing the nashorn-compat flag. Did you check you actually NEED it? This is a legacy feature that you should only use if you really have to; it might lock you out of newer features of newer JavaScript (ECMAScript) features on the long run. See https://github.com/graalvm/graaljs/blob/master/docs/user/NashornMigrationGuide.md if you are using a feature that actually requires strict Nashorn compatibility.

Best,
Christian

Thank you both, I got the script running using the "proxy" solution. Now I get some other errors but at least it is running.

@wirthi , I will test if it runs without nashorn-compat, I'm using for each in my script so I'm unsure if that is a nashorn extension or not.

Was this page helpful?
0 / 5 - 0 ratings