Rhino,从多个 javascript 文件添加代码

发布于 2024-09-06 02:26:19 字数 346 浏览 7 评论 0原文

我正在使用 Rhino 在 Java 应用程序中嵌入一些 javascript。我按照Rhino网站上的示例,通过调用Context的evaluateString方法并将实际脚本作为字符串传递来执行脚本。

我有一大堆现有的 javascript 代码,我想利用它们。我不想将其全部连接成一个巨大的字符串并将其传递给evaluateString。我宁愿能够加载代码,以便我可以从传递到评估字符串的代码中调用它(有点像 Microsoft 脚本控件中的 AddCode 方法)。我想添加代码,就像我当前可以使用 ScriptableObject.putProperty 方法添加变量一样。

有办法做到这一点吗?有人可以提供代码片段或文档链接吗?谢谢!

I am embedding some javascript in a Java application using Rhino. I am following the example on the Rhino website, executing a script by calling the Context's evaluateString method and passing the actual script in as a String.

I have a whole bunch of existing javascript code that I would like to make use of. I don't want to concatenate it all into an enormous String and pass it in to evaluateString. I would rather be able to load the code in so that I can call it from the code that I do pass into evaluateString (kind of like the AddCode method works in Microsoft's scripting control). I would like to add code like I can currently add variables by using the ScriptableObject.putProperty method.

Is there a way to do this? Can someone provide a code snippet or a link to the documentation. Thanks!

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

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

发布评论

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

评论(2

甜嗑 2024-09-13 02:26:19

来自文档示例 看起来对先前评估的对象的引用是由 范围

Context context = Context.enter();
try {
  ScriptableObject scope = context.initStandardObjects();
  Object out = Context.javaToJS(System.out, scope);
  ScriptableObject.putProperty(scope, "out", out);
  context.evaluateString(scope,
      "function foo() { out.println('Hello, World!'); }", "<1>", 1, null);
  context
      .evaluateString(scope, "function bar() { foo(); }", "<2>", 1, null);
  context.evaluateString(scope, "bar();", "<3>", 1, null);
} finally {
  Context.exit();
}

(Rhino 1.7 release 2)


我知道有些人直接使用Rhino来获取最新版本,但是Java 6 实现 可以像这样评估脚本:

ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
engine.eval("function foo() { println('Hello, World!'); }");
engine.eval("function bar() { foo(); }");
engine.eval("bar();");

From the documentation and examples it looks like references to previously evaluated objects are controlled by scopes.

Context context = Context.enter();
try {
  ScriptableObject scope = context.initStandardObjects();
  Object out = Context.javaToJS(System.out, scope);
  ScriptableObject.putProperty(scope, "out", out);
  context.evaluateString(scope,
      "function foo() { out.println('Hello, World!'); }", "<1>", 1, null);
  context
      .evaluateString(scope, "function bar() { foo(); }", "<2>", 1, null);
  context.evaluateString(scope, "bar();", "<3>", 1, null);
} finally {
  Context.exit();
}

(Rhino 1.7 release 2)


I know some people use Rhino directly to get the latest version, but the Java 6 implementation can evaluate scripts like this:

ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
engine.eval("function foo() { println('Hello, World!'); }");
engine.eval("function bar() { foo(); }");
engine.eval("bar();");
瀟灑尐姊 2024-09-13 02:26:19

在我的代码中,我有这种需求(实用程序脚本等),我只是简单地将它们连接到一个巨大的 StringBuilder 中并对其进行评估(Java 6)。这是因为 javascript 无法执行 otherJSScript.someUsefulFunction() (没有 Java 包装对象)的唯一方法。

In my code I had that need (utility scripts and such), and I just simply concatenated them together in a giant StringBuilder and evaled it (Java 6). Its the only way since javascript can't do (without Java wrapper objects) otherJSScript.someUsefulFunction().

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