javarenjin

Is a Renjin Session reentrant


A Session is Renjin is not thread safe as described here, but is it reentrant safe ?

The scenario is calling from java engine.eval("...") that has a Java class that calls again the same engine.eval("..") method. Let's assume we've only one engine instance for the sake of simplicity.


Solution

  • Yes, that is possible.

    Note that by calling engine.eval() on the original ScriptEngine instance, the expression will be evaluated in the global environment and the R function won't be able to see the R call stack that invoked the Java method.

    You can also ask Renjin to pass the current Context to your Java method when invoked. For example:

    class MyJavaClass {
       static SEXP estimate(@Current Context context, SEXP function) {
          return context.evaluate(FunctionCall.newCall(function, IntVector.valueOf(42)));
       }
    }
    

    And then:

    import(MyJavaClass)
    f <- function(x) x*2
    MyJavaClass$estimate(f)  # 84