G B
G B

Reputation: 3024

Call javascript interpreter from a script

I've written some scripts in Javascript under Rhino 1.7, one of them starts a minimal http server and accepts JS commands in input.

Now, if I call (from within Rhino):

engine = ScriptEngineManager().getEngineByName("JavaScript");

I get the builtin JS engine (from Java 1.6), that is an older version of Rhino, and lacks some functions (like JavaAdapter for multiple interfaces).

How do I get the Rhino Engine instead of that? Do I need ScriptEngineManager.getEngineFactories() or what else?

Upvotes: 0

Views: 583

Answers (3)

G B
G B

Reputation: 3024

I found it out myself (trial and error). As noted above, Rhino doesn't register an engine factory. You can get the current engine (as a context and a scriptable object):

cx = Context.getCurrentContext();
scope = new ImporterTopLevel(cx);

With these objects, I can run my scripts or command lines using evalString/evalReader.

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328594

What you want to achieve is to select a certain version of an script engine which implements "JavaScript". The correct way to do that is to call ScriptEngineManager.getEngineFactories() and then check the results of getLanguageName() and getEngineVersion().

Upvotes: 1

Kevin
Kevin

Reputation: 30419

Before invoking your initial script, why don't you set the engine you're using as a context variable inside the script? That way, inside the script, you'll have access to the engine that is running it.

Upvotes: 0

Related Questions