从主持人语言注入的成员将以来宾语言类型到达来宾语言 [英] Injected members from the host language to arrive to guest language as guest language types

查看:89
本文介绍了从主持人语言注入的成员将以来宾语言类型到达来宾语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题与以下内容有关: GraalVM-在没有上下文的情况下使用Polyglot值

This question is somewhat related to: GraalVM - Using Polyglot Value without a context

在我的应用程序中,以来宾语言运行的代码片段不需要知道注入的参数(通过绑定的成员)是Java参数.相反,对于使用来宾语言开发的人,参数应该看起来像来宾语言本身的另一个参数.

In my application, the code snippets run in the guest languages should not need to know that the arguments injected (through members of the bindings) are Java arguments. Instead, for people developing in the guest language, arguments should look like just another argument of the guest language itself.

例如,我希望以这种方式从Java宿主语言向JS来宾脚本中注入一个数组:

For example, I would like for an array injected from my Java host language to a JS guest script, in this fashion:

Value guestLanguageBindings = context.getBindings(scriptData.getLanguage().toString());

guestLanguageBindings.putMember(argumentName, argumentValue);

作为JS数组到达"来宾语言,而不是作为Java.util.ArrayList来到达"来宾语言.

to "arrive" to the guest language as a JS array, not as a java.util.ArrayList as it is happening right now.

当前,我通过将每个非原始类型(我注意到String,int等作为JS类型"到达JS)转换为JSON并转换为来宾语言来解决此问题.

Currently I got around this problem by converting every non primitive type (I noticed that String, int, etc. arrive to JS as JS "types") to JSON and converting back in the guest language.

这行得通,但是我想知道是否有更合适的方法来做,或者确实使用绑定是正确的方法?

This works, but I wonder if there is a more appropriate way to do it or if indeed using the bindings is the right way to go?

谢谢!

推荐答案

@Christian Humer提供的建议使用ProxyArray的答案是一个很好的答案,它说明了如何正确组合ProxyArray并得到了很好的解释.

The answer provided by @Christian Humer suggesting to use ProxyArray is a good one, and is explanation on how to put together a ProxyArray is correct and well presented.

但是,我的要求是要能够将数组参数(使用顶级绑定设置)呈现为一种来宾语言数据类型. ProxyArray仅使我半途而废,结果数据类型是Java类型,而不是JS类型.

However, my requirement was to be able to present the array argument (set using the top level bindings) as if it was a guest language data type. ProxyArray only get me halfway there, with the resulting data type being a Java type and not for example a JS type.

为了完全实现上述目标,由于我控制着Java主机端,因此我在主机端使用Context创建了一个JS数组,并在其中复制了Java ArrayList的内容. .调用访客代码时,我只需在绑定中设置JS数组,该数组已经是功能齐全的JS数组.

In order to fully achieve the above, since I am in control of the Java host side, I have created up-front a JS array using the Context, on the host side, and copied the content of the Java ArrayList in it. When calling the guest code, I simply set the JS array in the bindings, which is already a fully featured JS array.

    Value jsArray = context.eval("js", "new Array();");

    jsArray.setArrayElement(0, 1001); //array will grow automatically, JS semantics
    jsArray.setArrayElement(1, 1002);
    jsArray.setArrayElement(2, 1003);

    guestLanguageBindings.putMember("variable", jsArray);
    context.eval("js", "print(variable);");

当我提交提到的错误报告时,在Github上向我建议了 在上面的评论中.

This approach was suggested to me on Github when I filed the bug report mentioned in the comments above.

这篇关于从主持人语言注入的成员将以来宾语言类型到达来宾语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆