简体   繁体   English

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

[英]interoperation between Rhino and Java via JSR223: working with Javascript Object instances

This is very similar to this other SO question about arrays . 这与关于数组的其他SO问题非常相似。

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. 在通过JSR223(ScriptingEngine)实例化的Javascript脚本中,我得到了NativeObject (我在Eclipse的调试器中看到了), NativeObject知道如何访问其属性。 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. 此外,我什至不知道哪个.jar文件(如果有的话),我需要添加到我的构建路径中才能使用相关的类,并且如果我找到了可以在Rhino Javascript中使用的方法,则对于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. 似乎JSR223应该包括对ScriptingEngine的语言不可知的访问方法,以提供将返回的对象包装为数组的List<Object>或关联数组的Map<String, Object>功能。

Any suggestions? 有什么建议么?

I too am trying to embed different scripting languages with more features than jsr223 or bsf. 我也试图嵌入比jsr223或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. 我想要的一个功能是能够将Function(具有单个方法的Java接口)传递给脚本引擎,并使其在传递参数时可以正常工作。 Each of my embedded scripting engines has a layer where i wrap/unwrap from/to java values from the scripting environment. 我的每个嵌入式脚本引擎都有一个层,在该层中,我可以从脚本环境中包装Java值/将包装值解包。

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. 我建议解决此问题的最佳方法是让您的脚本引擎包装器提供getValue(String name)并修复将其转换为Java List的javascript数组。 Naturally the setValue(String, Object) would check if the value is a List and convert it back to a js array and so on. 自然,setValue(String,Object)将检查该值是否为List并将其转换回js数组,依此类推。 Its tedious :() 它乏味的:()

Convert it to a java object and return it. 将其转换为java对象并返回。 You can then work with the java object as you would normally. 然后,您可以像平常一样使用java对象。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM