This page contains implementation notes about the Nashorn Host.
See the Nashorn Host page for more user-friendly documentation.
SetTimeout
Nashorn does not define setTimeout(), setInterval() etc., below are some possible solutions
- 2013 Oracle Blog with March 2014 updates
- Uses javafx.application.Platform, which is rather Oracle-JDK-centric and possibly not preferred.
- salomvary gist November 2014
// Adopted from here: https://gist.github.com/bripkens/8597903
// Makes ES7 Promises polyfill work on Nashorn https://github.com/jakearchibald/es6-promise
// (Haven't verified how correct it is, use with care)
- bripkens gist June 2014
Java 8 Nashorn event loop "polyfill". javax.script.ScriptEngine#eval calls should immediately call window.main to enter the event loop and thus to avoid concurrency issues. The XMLHttpRequest simulation is not yet finished.
- kayhadrin gist Oct 2016
- Does not use javafx.application.Platform
- Has a link to josmardias
- This one does not properly terminate. That is, running
(cd $PTII/org/terraswarm/accessor/accessors/web/hosts; $PTII/bin/ptinvoke ptolemy.actor.lib.jjs.NashornAccessorHostApplication -js -timeout 5000 common/test/testCommon.js)
does not exit.
- josmardias gist Aug 2016
// https://gist.github.com/josmardias/20493bd205e24e31c0a406472330515a
// at least one timeout needs to be set, larger then your code bootstrap
// or Nashorn will run forever
// preferably, put a timeout 0 after your code bootstrap
- It is necessary to call setTimeout() after bootstrapping, which we do in NashornAccessorHostApplication.java:
int timeout;
...
Object instance = engine.eval("function() { print('NashornAccessorHostApplication done.');}");
((Invocable)engine).invokeFunction("setTimeout", instance, timeout);
- Use this one
commonHost and setTimeout etc.
Edward suggested that we can define setTimeout in commonHost and keep track of requests in commonHost.
Then, to stop an accessor, we cancel the timeouts and intervals.
Differences with Cape Code
Cape Code has two JavaScript globals: actor
and accessor
that are set to the JavaScript actor.
capeCodeHost.js invokes actor.setTimeout()
etc. However, in Nashorn, we don't have an actor.
For Nashorn, if actor
is null, then we could have
if (typeof actor === 'undefined' || actor === null) {
var actor = require('js-timeout-polyfill.js');
fi
console
Nashorn does not have console, but it does have print.
See Also