Java 的 JavaScript 引擎

发布于 2024-10-12 05:17:20 字数 310 浏览 4 评论 0原文

目前我在我的应用程序中使用Rhino。我需要评估一些 JavaScript ant 从中获取值(我不需要通过 JavaScript 使用 Java 类)。但速度太慢了。也许有什么方法可以将 V8 与 Java 应用程序一起使用?

更新:

我有大量不同类型的对象。我需要一个灵活的机制来验证和转换这些对象到所需的形式(用户应该能够更改验证和转换的规则(在运行时),即在Java中硬编码这些规则不合适)。 现在,Rhino 上一切正常,但性能很差。我想过使用 NodeJS,但似乎与它通信、通过进程进行对象序列化等等 - 这些都会花费很大。

Currently I'm using Rhino in my application. I need to eval some JavaScript ant get values from it (I don't need to use Java classes through JavaScript). But it is too slow. Maybe there are any ways to use V8 with Java application?

Update:

I have a large collection of objects of different types. I need a flexible mechanism for validation and transformation these objects to the required form (the user should be able to change the rules of validation and transformation (in runtime), ie hardcoding these rules in Java not suitable).
Now everything works on Rhino, but performance is bad. I thought of using NodeJS, but it seems that communication with it, object serialization through processes, etc. - these all will cost very much.

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

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

发布评论

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

评论(2

嗫嚅 2024-10-19 05:17:20
  1. 绝对确定您确实需要eval吗?实际上需要 eval 的地方非常非常少。

  2. 您可以使用ProcessBuilder 向底层系统可用的任何进程进行 shell 处理。我想说它比 Rhino eval 更快的可能性很低。

  3. 您可以让 NodeJS 进程与您的应用程序一起运行,您通过插座。 可能会在 Rhino 中赢得与 eval 的速度竞赛。

如果你给出一个你实际想要实现的目标的例子,人们可能会为你想出更好的方法。

  1. Are you absolutely sure you really need eval? There are very, very few places where eval is actually necessary.

  2. You can use ProcessBuilder to shell out to any process available to the underlying system. I'd say the odds of it being faster than a Rhino eval are low.

  3. You might keep a NodeJS process running alongside your app which you communicate with via a socket. That might win a speed race with eval in Rhino.

If you give an example of what you're actually trying to achieve, it may be that people can come up with a better approach for you.

撧情箌佬 2024-10-19 05:17:20

看一下javax.script.ScriptEngine。它是一个标准的 Java 包,允许评估和数据绑定:

ScriptEngineManager engineMgr = new ScriptEngineManager();
ScriptEngine engine = engineMgr.getEngineByName("JavaScript");
Bindings bindings = engine.createBindings();

String script = "javascript to eval goes here.....";
bindings.put(varName1, value1);
bindings.put(varName2, value2);

Object obj = engine.eval(script, bindings)

Have a look at javax.script.ScriptEngine. It's a standard Java package, allows evals and data binding:

ScriptEngineManager engineMgr = new ScriptEngineManager();
ScriptEngine engine = engineMgr.getEngineByName("JavaScript");
Bindings bindings = engine.createBindings();

String script = "javascript to eval goes here.....";
bindings.put(varName1, value1);
bindings.put(varName2, value2);

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