从脚本调用 javascript 解释器

发布于 2024-08-12 16:42:25 字数 339 浏览 6 评论 0原文

我在Rhino 1.7下用Javascript编写了一些脚本,其中一个启动一个最小的http服务器并接受输入中的JS命令。

现在,如果我调用(从Rhino内部):

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

我得到内置的JS引擎(来自Java 1.6),这是Rhino的旧版本,并且缺少一些功能(例如用于多个接口的JavaAdapter)。

我如何获得 Rhino 引擎而不是它?我需要 ScriptEngineManager.getEngineFactories() 还是其他什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁客 2024-08-19 16:42:25

您想要实现的是选择实现“JavaScript”的脚本引擎的某个版本。正确的方法是调用 ScriptEngineManager.getEngineFactories(),然后检查 getLanguageName()getEngineVersion() 的结果。

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

执手闯天涯 2024-08-19 16:42:25

我自己发现了这一点(反复试验)。如上所述,Rhino 没有注册引擎工厂。您可以获得当前引擎(作为上下文和可编写脚本的对象):

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

使用这些对象,我可以使用 evalString/evalReader 运行我的脚本或命令行。

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.

灯角 2024-08-19 16:42:25

在调用初始脚本之前,为什么不在脚本内将正在使用的引擎设置为上下文变量呢?这样,在脚本内部,您就可以访问运行它的引擎。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文