Issue in Renjin ScriptEngine: it does not redirect streams

61 views
Skip to first unread message

Horváth Péter Gergely

unread,
Jun 9, 2016, 4:27:39 AM6/9/16
to renji...@googlegroups.com
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






Alexander Bertram

unread,
Jun 10, 2016, 5:29:11 AM6/10/16
to Renjin Contributors, peter.gerg...@gmail.com
Hi Peter,
Thanks for the clear test cases!
I've open Issue #188 in our tracker:

I've committed a fix based on what you sugggested above a new release should be available shortly.

Best,
Alex

Horváth Péter Gergely

unread,
Jun 10, 2016, 6:24:42 AM6/10/16
to Alexander Bertram, Renjin Contributors, Péter Gergely Horváth
Hi Alex,

Thank you very much for your help and the quick turn-around time. 

I have one additional question: do you have any plan to promote Renjin to Maven Central repository? I have a small project [1], for which I have an integration test for Renjin [2], which - once it becomes a bit more mature -, I would like to push to Maven Central repository. Having an external dependency there is discouraged. 


Thanks,
Peter

Alexander Bertram

unread,
Jun 14, 2016, 5:47:56 AM6/14/16
to Horváth Péter Gergely, Renjin Contributors
Hi Peter,
Looks like an interesting project!

I think it would be good to eventually promote Renjin to Maven Central, but right now are releasing 2-3 times a week and I'm not sure what kind of burden that would entail. 

In the meantime, perhaps you can make renjin only a test-scoped dependency of your project? Or create a separate integration-test profile that only adds the Renjin repository when activated? 

Best,
Alex
Reply all
Reply to author
Forward
0 new messages