zoukankan      html  css  js  c++  java
  • graalvm java map 参数传递js 方法调用

    因为js 有自己的特性,我们一般对于map 参数的传递通过ProxyObject.fromMap 处理,以下是一个demo

    基本使用

    • 参考代码
    public static  void es6Rollup2(Engine engine) throws IOException {
            Context context = Context.newBuilder().allowAllAccess(true).allowHostClassLoading(true).allowIO(true).allowNativeAccess(true).engine(engine).build();
            Source mysource = Source.newBuilder("js","load("src/main/resources/mydemoapp.js");","demoapp").mimeType("application/javascript+module").build();
            context.eval(mysource);
            Value callapp = context.getBindings("js").getMember("window");
            Value result = callapp.getMember("demo");
            Map<String,Object> ob =new HashMap<>();
            Map<String,Object> ob2 =new HashMap<>();
            ob2.put("url","https://plus.google.com/102817283354809142195/posts/F97fqZwJESL");
            ob.put("url","https://plus.google.com/102817283354809142195/posts/F97fqZwJESL");
            Map<String,Object> ob3 =new HashMap<>();
            ob3.put("url","https://plus.google.com/102817283354809142195/posts/F97fqZwJESL");
            ob2.put("ob",ProxyObject.fromMap(ob3));
            ob.put("id","z12gtjhq3qn2xxl2o224exwiqruvtda0i");
            ob.put("object",ProxyObject.fromMap(ob2));
            String filed = "url,object(url,ob/url)";
            System.out.println(ob.toString());
            String info ="{"id":"z12gtjhq3qn2xxl2o224exwiqruvtda0i","url":"https://plus.google.com/102817283354809142195/posts/F97fqZwJESL","object":{"objectType":"note","content":"A picture... of a space ship... launched from earth 40 years ago.","attachments":[{"objectType":"image","url":"http://apod.nasa.gov/apod/ap110908.html","image":{"height":284,"width":506}}]},"provider":{"title":"Google+"}}";
            Value execResult =result.execute(ProxyObject.fromMap(ob),filed);
            System.out.println(execResult);
        }
    • 说明
      通过ProxyObject处理,但是因为默认ProxyObject.fromMap 不会进行深复制处理,如果我们是嵌套的就需要多次处理
      同时以上的js 代码如下(使用了json-mask 处理json 内容,java 调用通过browserify 进行了转换,具体可以参考https://www.cnblogs.com/rongfengliang/p/13584488.html)
     
    const mask = require("json-mask")
    module.exports = {
        maskfn: function (datas, fields) {
            return mask(datas, fields)
        }
    }
    window = this || {}
    window.demo = function (datas, fields) {
        let mydatas 
        mydatas = datas;
        if (typeof datas === 'string') {
            mydatas =JSON.parse(datas)
        } 
        return mask(mydatas, fields)
    }
    • 运行效果

    说明

    实际上官方也有相关的特性开关,可以实现自动的处理,还没测试,后边测试下

    参考资料

    https://github.com/graalvm/graaljs/issues/143

  • 相关阅读:
    方维P2P  二次开发
    Array 数组去重 总结10方法(7)
    PHP  OOP学习总结
    [转载]js:数组里面获取键名和键值
    Array对象的方法实现(6)----Array.prototype.indexOf(实现常规参数的功能)
    在Apache服务器上启用GZip压缩静态内容的方法
    PHP 程序授权验证开发思路
    【转】zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法
    公钥私钥,HTTPS,CA证书机构,单向和双向认证
    Array对象的方法实现(5)----Array.prototype.includes(实现常规参数的功能)
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/13585334.html
Copyright © 2011-2022 走看看