开发者

Call javascript interpreter from a script

开发者 https://www.devze.com 2022-12-12 17:28 出处:网络
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.

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 开发者_C百科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?


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().


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消