Creating Generic Rhinocript Objects from Java / Scala

I am trying to improve javascript fragment evaluator performance. These script fragments can refer to any number of variables that exist in the string map of json-like object graphs (IE: Json AST). I use JDK 1.6 and the built-in Rhinocript engine (v1.6R2).

Currently processing takes the form of:

  • Parses a fragment to find the names of reference variables
  • Variables are extracted from the map and serialized to json string
  • Json string is assigned to the same named variable at the beginning of the script
  • Rate advanced script

I am trying to figure out how to skip the json serialization step and create direct Rhinocript siblings for placement in the 'bindings' for the script. Then the required steps are:

  • Parses a fragment to find the names of reference variables
  • Variables are extracted from the map and converted to native Rhinocript equivalents
  • Own objects are placed in bindings
  • Evaluate source script with specified bindings

Do you know where I can find documentation or examples of how to instantiate your own rhinocript?

My scala training project can be useful if you want to mess around. And any answer that I came up with should appear there too ...

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

Thanks in advance.

+1
source share
1 answer

So, after repeated play, I came up with an adequate solution, although this did not lead to the productivity increase I was hoping for (only 10% faster).

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

Summarizing:

  • Context.javaToJs () does not seem to work in all cases. ( java.lang.RuntimeException: No Context associated with current Thread )
  • Requires a "scope" object of type Scriptable . The solution I came up with is not very pretty, but it works.
  • To create a NativeArray:

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

Source: https://habr.com/ru/post/888434/


All Articles