Cannot run Nashorn and JSScripting in parallel

openHAB 4.1.0.

I have seen posts with screenshots here and also in the docs, suggesting that one can still have Nashorn for old JS scripts and also have the new JSScripting installed. Then one can choose which one to use for inline scripts in rules (different mime types).

So, I tried to accomplished that and destroyed by OH installation. Luckily, I had a recent backup.

This is what I did:
I had installed JavaScript Scripting (Nashorn) [3.3.0.0;4.0.0.0).
I had not installed JavaScript Scripting (Nashorn) 4.1.0.
I had not installed JavaScript Scripting.

Then I installed JavaScript Scripting 4.1.0. Then I was able to use new features in a rule script.
However, for new rules, there was no choice between Nashorn and the new JS engine.
Old scripts started and failed. Also, transformations with ā€œ.jsā€ files failed because no engine for ā€œ.jsā€ files.

So, I uninstalled JavaScript Scripting 4.1.0 again, but this did not fix the issues.
Then i thought, maybe uninstall and reinstall Nashorn would help.
I uninstalled the 3.3.0.0;4.0.0.0 version of Nashorn but then the add-on with this version was not available anymore to reinstall. So I installed Nashorn 4.1.0.
This still did not fix the issues with existing scripts and JS transformations. Nashorn 4.1.0 seemingly only wants to handled scripts with the new mime type ā€œapplication/javascript;version=ECMAScript-2021ā€.

Then I gave up and restored the backup.

Can anyone please explain how I get to the point that I have Nashorn still working and handling mime type ā€œapplication/javascriptā€, including JS transformations in .js files and also be able to create new rule scripts with mime type ā€œapplication/javascript;version=ECMAScript-2021ā€?

Thanks!

I believe you misread those.
And to be frank it is a bad idea to attempt at all youā€™re creating a barely maintainable mess of a system.
Better migrate your Nashorn rules right away. Less work and trouble in the end.

1 Like

Maybe, but please see this screenshot from the docs:

From JavaScript Scripting - Automation | openHAB.

So, I should uninstall the old Nashorn, install JSScripting and convert every script in one go? No soft migration possible?

You need to make sure that the rule ā€žknowsā€œ whether to execute the code as Edition 5.1 or Edition 11 (in reference to your screenshot).
For each single rule click in the config settings on the ā€œ>ā€œ symbol of this setting:

execute an inline script        >

and select the correct edition. They should work now.
This way you can migrate your rules 1:1 over time.

I would have expected that, but after installing JSScripting, there was only ONE option and that was the new version.

Did you reinstall Nashorn from Addon-Store?

This happened:

  • old Nashorn (3.0.0-4.0.0) was installed
  • installed JSScripting
  • old Nashorn was still installed
  • old rules do not work anymore, no choice of interpreter for new rules
  • uninstalled JSScripting
  • still old rules do not work anymore, so I thought Iā€™d reinstall Nashorn
  • uninstalled old Nashorn
  • installed new Nashorn 4.1.0 (because old one was not available anymore)
  • still old rules do not work anymore, because new Nashorn looks at new mime type
  • restored backup

At this point you have the correct add-ons installed. The 3.3.0;4.0.0.0 version is only for OH 3. You need the Nashorn 4.1 add-on.

Correct. ā€œapplication/javascriptā€ will be executed using the JS Scripting addon. Thatā€™s how you tell OH which automation add-on to run that script. One of the breaking changes the release announcement for OH 4 discusses is you need to change the mime type of your Nashorn scripts to continue to use Nashorn JS.

Your JS Script transformations should work though. The old Nashorn transformations should be compatible with the new GraalVM JS.

You canā€™t. You have to change the mime type to have them run on Nashorn.

You canā€™t you have to use ā€œapplication/javascriptā€ as the mime type for the new JS Scripting addon.

Thatā€™s because at that point you had the wrong and a non-working version of the Nashorn add-on installed.

tl;dr:

  • You must remove the Nashorn 3.3.0.0;4.0.0.0 add-on installed from the Marketplace
  • You must install the JS Scripting automation add-on from the add-on store
  • You must install the Nashorn automation add-on from the add-on store
  • You must set the mime type for the Nashorn scripts to ā€œapplication/javascript;version=ECMAScript-5.1ā€
  • You must set the mime type for the JS Scripting scripts to ā€œapplication/javascriptā€
  • Your JS transformations should work as is once the correct add-ons are installed; but note that they will be executed using the JS Scripting add-on. You have to change the JS(...) to NASHORNJS() to have them executed with the Nashorn add-on.
2 Likes

Very cool, Rich. Thanks for the great explanation! Iā€™ll do that at the weekend.

This was the transformation that was failing:

2024-03-21 07:15:54.500 [ERROR] [.module.script.profile.ScriptProfile] - Failed to process script ā€˜bytes.jsā€™: Script type ā€˜jsā€™ is not supported by any available script engine.

This is the script ā€œbytes.jsā€:

(function(i) {
    return i + "B";
})(input)

I guess, if all add-ons are installed correctly, this should still work.

Yes, but if not come back with the code and logs. The error indicates at the time this ran you did not have a working Nashorn add-on installed.

1 Like

Just wanted to give some feedback.

Everything worked exactly as @rlkoshak wrote. After installation and changing the rule mime types, all old Nashorn scripts and transformations still worked. The simple transformation scripts did not need to be changed to the Nashorn mime type.

I have now also converted some old DSL rules (yes, still also have those) to the new JSScripting.

So far, so good. :slight_smile: