如何使用 Apache Rhino 调用子属性函数

发布于 2024-12-03 22:38:00 字数 586 浏览 7 评论 0原文

如果我有一个如下所示的 js 对象存储在 js 文件中,

var _sampleProcessor = {
    process: function(data){
        ...
    }

}

我将如何使用 Apache Rhino 来调用 process 函数?

// sb holds the contents of the js file
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, sb.toString(), "Test", 1, null);

Object processor = scope.get("sampleProcessor ", scope);
if (processor  == Scriptable.NOT_FOUND) {
    System.out.println("processor  is not defined.");
}

到达对象的根很容易,但是我如何遍历对象树以获取过程函数属性

提前致谢

If i have a js object such as below stored in a js file

var _sampleProcessor = {
    process: function(data){
        ...
    }

}

How would i use Apache Rhino to call the process function?

// sb holds the contents of the js file
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, sb.toString(), "Test", 1, null);

Object processor = scope.get("sampleProcessor ", scope);
if (processor  == Scriptable.NOT_FOUND) {
    System.out.println("processor  is not defined.");
}

Getting to the root of the object is easy, but how do i traverse the object tree to get at the process function property

Thanks in advance

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

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

发布评论

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

评论(1

脱离于你 2024-12-10 22:38:01

该示例做了一些事情。像您的示例一样提取 sampleProcessor,并且还提取 process 属性并执行该函数。

它还展示了将 Java 对象添加到作用域中以便可以使用它们 - 示例中的 System.out 对象。

package grimbo.test.rhino;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class InvokeFunction {
    public static void main(String[] args) {
        String sb = "var sampleProcessor = {\n" + "    process: function(data){\n    out.println(0); return 1+1;\n    }\n" + "}";

        Context cx = Context.enter();
        Scriptable scope = cx.initStandardObjects();

        Object out = Context.javaToJS(System.out, scope);
        ScriptableObject.putProperty(scope, "out", out);

        cx.evaluateString(scope, sb.toString(), "Test", 1, null);

        // get the sampleProcessor object as a Scriptable
        Scriptable processor = (Scriptable) scope.get("sampleProcessor", scope);
        System.out.println(processor);

        // get the process function as a Function object
        Function processFunction = (Function) processor.get("process", processor);
        System.out.println(processFunction);

        // execute the process function
        Object ob = cx.evaluateString(scope, "sampleProcessor.process()", "Execute process", 1, null);
        System.out.println(ob);
    }
}

输出:

[object Object]
org.mozilla.javascript.gen.Test_1@b169f8
0.0
2

This sample does a few things. Pulls out the sampleProcessor as your example does, and also pulls out the process property and executes that function.

It also shows adding Java objects into the scope so they can be used - the System.out object in the example.

package grimbo.test.rhino;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class InvokeFunction {
    public static void main(String[] args) {
        String sb = "var sampleProcessor = {\n" + "    process: function(data){\n    out.println(0); return 1+1;\n    }\n" + "}";

        Context cx = Context.enter();
        Scriptable scope = cx.initStandardObjects();

        Object out = Context.javaToJS(System.out, scope);
        ScriptableObject.putProperty(scope, "out", out);

        cx.evaluateString(scope, sb.toString(), "Test", 1, null);

        // get the sampleProcessor object as a Scriptable
        Scriptable processor = (Scriptable) scope.get("sampleProcessor", scope);
        System.out.println(processor);

        // get the process function as a Function object
        Function processFunction = (Function) processor.get("process", processor);
        System.out.println(processFunction);

        // execute the process function
        Object ob = cx.evaluateString(scope, "sampleProcessor.process()", "Execute process", 1, null);
        System.out.println(ob);
    }
}

Output:

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