通过 JSR223 在 Rhino 和 Java 之间进行互操作:使用 Javascript 对象实例

发布于 2024-10-31 03:22:41 字数 643 浏览 1 评论 0原文

这与 另一个关于数组的问题。

如果我评估:

y = {a: 1, b: 2, "momomomo": function() { return "hi"; }, zz: "wham"}

在通过 JSR223 (ScriptingEngine) 实例化的 Javascript 脚本中,我会得到某种 NativeObject (我在 Eclipse 的调试器中看到它),并且不知道如何访问其属性。此外,我什至不知道哪个 .jar 文件(如果有)需要添加到我的构建路径中才能使用相关类,并且如果我找到一种适用于 Rhino Javascript 的方法,那么它对于吉通。

似乎 JSR223 应该包含对 ScriptingEngine 的与语言无关的访问方法,以提供将返回的对象包装为数组的 ListMap 用于关联数组。

有什么建议吗?

This is very similar to this other SO question about arrays.

If I evaluate:

y = {a: 1, b: 2, "momomomo": function() { return "hi"; }, zz: "wham"}

in a Javascript script instantiated via JSR223 (ScriptingEngine), I get a NativeObject of some sort (I see this in Eclipse's debugger) and have no idea how to access its properties. Furthermore I don't even know which .jar file, if any, I need to add to my build path to be able to work with the class in question, and if I find an approach that works in Rhino Javascript, it is useless for Jython.

Seems like JSR223 should have included language-agnostic access methods to ScriptingEngine to provide the ability to wrap a returned object as a List<Object> for arrays or a Map<String, Object> for associative arrays.

Any suggestions?

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

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

发布评论

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

评论(2

情丝乱 2024-11-07 03:22:41

我也尝试嵌入不同的脚本语言,其功能比 jsr223 或 bsf 更多。为此,我必须定义自己的接口并围绕每个不同的脚本引擎实现这些接口。

我想要的一个功能是能够将函数(具有单个方法的 java 接口)传递给我的脚本引擎,并让它在传递参数时正常工作。我的每个嵌入式脚本引擎都有一个层,我可以在其中从脚本环境中包装/解开 java 值。

我建议解决问题的最佳方法是让脚本引擎的包装器提供 getValue( String name ) 并让它修复 javascript 数组,将它们转换为 java 列表。当然,setValue(String, Object) 会检查该值是否是一个 List 并将其转换回 js 数组等等。很乏味:()

I too am trying to embed different scripting languages with more features than jsr223 or bsf. For that i have had to define my own interfaces and implement thse around each different scripting engine.

One feature i wanted was the ability to pass a Function (java interface with a single method) to my scripting engine and have it just work when passed parameters. Each of my embedded scripting engines has a layer where i wrap/unwrap from/to java values from the scripting environment.

I would suggest the best way to solve the problem is for your wrapper around the scripting engine to provide a getValue( String name ) and have it fix up javascript arrays convertoing them to java Lists. Naturally the setValue(String, Object) would check if the value is a List and convert it back to a js array and so on. Its tedious :()

海夕 2024-11-07 03:22:41

将其转换为java对象并返回。然后您可以像平常一样使用 java 对象。

下面是一个转换函数的例子

function convertToJava(o) {
    var rval;
    if (Array.isArray(o)) {
        rval = new java.util.ArrayList();
        for (var key in o) {
            rval.add(convertToJava(o[key]));
        }
    } 
    else if (typeof o === 'object') {
        rval = new java.util.HashMap();
        for (var key in o) {
            rval.put(key, convertToJava(o[key]));
        }
    }
    else if (typeof o === 'function') {
        // skip
    }
    else if (typeof o === 'undefined') {
        // skip
    }
    else {
        rval = o;
    }
    return rval;
}

Convert it to a java object and return it. You can then work with the java object as you would normally.

The following is an example conversion function

function convertToJava(o) {
    var rval;
    if (Array.isArray(o)) {
        rval = new java.util.ArrayList();
        for (var key in o) {
            rval.add(convertToJava(o[key]));
        }
    } 
    else if (typeof o === 'object') {
        rval = new java.util.HashMap();
        for (var key in o) {
            rval.put(key, convertToJava(o[key]));
        }
    }
    else if (typeof o === 'function') {
        // skip
    }
    else if (typeof o === 'undefined') {
        // skip
    }
    else {
        rval = o;
    }
    return rval;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文