Hi All,
I have been experimenting with Renjin and I think I have found and issue. I would like to call the engine from a Java tool via the ScriptEngine API and I would need to capture the output generated by a script so that it can properly be displayed to the user.
Normally, ScriptEngines allow you to specify the stdout/stderr streams via the ScriptEngine API like the following:
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("foobar");
scriptEngine.getContext().setWriter(writer);
scriptEngine.getContext().setErrorWriter(writer);
scriptEngine.eval( ... );
After, this the evaluated script emits the output to the writer specified. Unfortunately, this does not seem to be the case for Renjin. It ignores the the standard settings and emits output to the standard output and error.
I have put together a test, which demonstrates the issue, in contrast to JavaScript ScriptEngine (Java 8 built-in / Nashorn), which correctly performs the redirection:
import org.testng.Assert;
import org.testng.annotations.Test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.StringWriter;
import java.io.Writer;
public class RenjinTest {
@Test
public void testJavaScriptScriptEngine() throws ScriptException {
testScriptEngine("JavaScript", "print('Hello World')");
}
@Test
public void testRenjinScriptScriptEngine() throws ScriptException {
testScriptEngine("Renjin", "cat('Hello World')");
}
private void testScriptEngine(String scriptEngineName, String helloWorldScript) throws ScriptException {
StringWriter stringWriter = new StringWriter();
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(scriptEngineName);
if(scriptEngine == null) {
throw new IllegalStateException("Could not load ScriptEngine: " + scriptEngineName);
}
Writer originalWriter = scriptEngine.getContext().getWriter();
Writer errorWriter = scriptEngine.getContext().getErrorWriter();
try {
scriptEngine.getContext().setWriter(stringWriter);
scriptEngine.getContext().setErrorWriter(stringWriter);
scriptEngine.eval(helloWorldScript);
} finally {
scriptEngine.getContext().setWriter(originalWriter);
scriptEngine.getContext().setErrorWriter(errorWriter);
}
Assert.assertEquals(stringWriter.toString().trim(), "Hello World");
}
}
I used the following dependencies (on Oracle Java 8, where Nashorn JavaScript ScriptEngine is present by default):
<dependency>
<groupId>org.renjin</groupId>
<artifactId>renjin-script-engine</artifactId>
<version>0.8.2062</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.21</version>
<scope>test</scope>
</dependency>
After doing some research, I have found that technically one can work-around this issue by performing the following:
PrintWriter printWriter = // ...
ScriptEngine scriptEngine = // ...
((org.renjin.script.RenjinScriptEngine) scriptEngine).getSession().getConnectionTable().getStdout().setOutputStream(printWriter);
((org.renjin.script.RenjinScriptEngine) scriptEngine).getSession().getConnectionTable().getStderr().setOutputStream(printWriter);
However, this means we loose all the benefits of having a non-implementation dependent ScriptEngine API. (Consider a generic tool, which can run multiple languages).
Would it be possible to have this issue fixed in the code base? Do you by any change know how should I request/report this?
Thank you,
Peter Horvath