Skip to content

Commit 8e8231d

Browse files
committed
Adapt an empty return value from eval to match Script Editor expectations
1 parent 124d5ae commit 8e8231d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/main/scala/org/scijava/plugins/scripting/scala/ScalaAdaptedScriptEngine.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ class ScalaAdaptedScriptEngine(engine: ScriptEngine) extends AbstractScriptEngin
2525
@throws[ScriptException]
2626
override def eval(script: String, context: ScriptContext): AnyRef =
2727
emulateBinding(context)
28-
evalInner(script, context)
28+
val r = evalInner(script, context)
29+
// Scala returns `Unit` when no value is returned. Script Engine (or the
30+
// Java side) expects `null` when no value was returned.
31+
// Anything else return as is.
32+
r match
33+
case _: Unit => null
34+
case x => x
2935

3036
private def emulateBinding(context: ScriptContext): Unit =
3137

src/test/java/org/scijava/plugins/scripting/scala/ScalaTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public void testBasic() throws Exception {
7474
}
7575
}
7676

77+
@Test
78+
public void testEmptyReturnValue() throws Exception {
79+
try (final Context context = new Context(ScriptService.class)) {
80+
final ScriptService scriptService = context.getService(ScriptService.class);
81+
final ScriptModule m = scriptService.run("hello.scala", "print(\"3\")", true).get();
82+
final Void expected = null;
83+
final Object actual = m.getReturnValue();
84+
assertEquals(expected, actual);
85+
}
86+
}
87+
7788
@Test
7889
public void testPutDouble() throws Exception {
7990
try (final Context context = new Context(ScriptService.class)) {

0 commit comments

Comments
 (0)