从Java/Scala实例化Rhinoscript本机物件 [英] Instantiating Rhinoscript Native Objects from Java/Scala

查看:63
本文介绍了从Java/Scala实例化Rhinoscript本机物件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试改善 javascript代码段评估程序的性能.这些脚本片段可以引用存在于类似json的对象图的字符串键映射中的任意数量的变量(IE:Json AST).我正在使用JDK 1.6和嵌入式Rhinoscript引擎(v1.6R2).

当前,处理采用以下形式:

  1. 分析摘录以发现引用变量的名称
  2. 从地图中检索变量并将其序列化为json字符串
  3. Json字符串在脚本开始时分配给了一个名称相似的变量
  4. 评估增强脚本

我试图弄清楚如何跳过json序列化阶段,并创建直接的Rhinoscript本机对象以放置在脚本的绑定"中.然后,所需的步骤将是:

  1. 分析摘录以发现引用变量的名称
  2. 从地图中检索变量,并转换为原生Rhinoscript等价物
  3. 本机对象置于绑定中
  4. 评估具有所述绑定的原始脚本

您知道吗,我在哪里可以找到文档或如何实例化本机犀牛对象的示例?

如果您想修改一下,我的scala学习项目可能会有所帮助.我想出的任何答案也应该出现在这里...

http://subversion.assembla. com/svn/freshcode_public/learn_scala/datastore/src/test/scala/pkg/script

谢谢.

解决方案

因此,经过反复摆弄之后,我提出了一个适当的解决方案,尽管它并没有达到我希望的性能提升(仅提高了10%). /p>

该解决方案特定于Scala/Lift/Json,包含在

  • 要创建NativeArray:

    val na = new NativeArray(arr)
    na.setPrototype(ScriptableObject.getClassPrototype(scope, "Array"))
    

  • I'm trying to improve the performance of a javascript snippet evaluator. These script snippets can reference any number of variables that exist in a string-keyed map of json-like object graphs (IE: Json AST). I'm using JDK 1.6 and the embedded Rhinoscript engine (v1.6R2).

    Currently, processing takes the form:

    1. Snippet is parsed to discover the names of referenced variables
    2. Variables are retrieved from the map and serialized to a json string
    3. Json string is assigned to a similarly named variable at the start of the script
    4. Evaluate augmented script

    I'm trying to figure out how to skip the json serializing stage and create direct Rhinoscript native objects to place in the 'bindings' for the script. The desired steps would then be:

    1. Snippet is parsed to discover the names of referenced variables
    2. Variables are retrieved from the map and converted to native Rhinoscript equivalents
    3. Native objects are placed in bindings
    4. Evaluation of original script with said bindings

    Do you know where I could find documentation or examples of how to instantiate native rhinoscript objects?

    My scala learning project might be of some use if you want to tinker. And any answer I come up with should appear there too...

    http://subversion.assembla.com/svn/freshcode_public/learn_scala/datastore/src/test/scala/pkg/script

    Thanks in advance.

    解决方案

    So after much fiddling I've come up with an adequate solution although it did not yield the performance increase I had hoped for (only 10% faster).

    The solution is specific to Scala / Lift / Json and is contained in the ScriptingUtil.scala file.

    To summarise:

    1. Context.javaToJs() does not seem to work in all cases. (java.lang.RuntimeException: No Context associated with current Thread)
    2. A 'scope' object of type Scriptable is needed. The solution I came up with isn't pretty but it works.
    3. To create a NativeArray:

      val na = new NativeArray(arr)
      na.setPrototype(ScriptableObject.getClassPrototype(scope, "Array"))
      

    这篇关于从Java/Scala实例化Rhinoscript本机物件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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