Skip to content

Commit 993712a

Browse files
committed
ScriptModule: populate the return value last
This change allows the return value (named "result") to be overridden as a declared, typed output parameter. Or if left undeclared, it exists as an implicit output of type java.lang.Object. This change hopefully prevents bugs surrounding (lack of) conversion of an output named "result" to its declared type.
1 parent 496f198 commit 993712a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/main/java/org/scijava/script/ScriptModule.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,11 @@ public void run() {
167167
}
168168

169169
// execute script!
170-
final ScriptLanguage language = getLanguage();
170+
Object returnValue = null;
171171
try {
172172
final Reader reader = getInfo().getReader();
173-
final Object returnValue;
174173
if (reader == null) returnValue = engine.eval(new FileReader(path));
175174
else returnValue = engine.eval(reader);
176-
setOutput(RETURN_VALUE, language.decode(returnValue));
177-
setResolved(RETURN_VALUE, true);
178175
}
179176
catch (Throwable e) {
180177
while (e instanceof ScriptException && e.getCause() != null) {
@@ -188,6 +185,7 @@ public void run() {
188185
}
189186

190187
// populate output values
188+
final ScriptLanguage language = getLanguage();
191189
for (final ModuleItem<?> item : getInfo().outputs()) {
192190
final String name = item.getName();
193191
if (isResolved(name)) continue;
@@ -197,6 +195,13 @@ public void run() {
197195
setOutput(name, typed);
198196
}
199197

198+
// populate the return value as a special output
199+
// NB: This only occurs if there was no declared output called "result".
200+
if (!isResolved(RETURN_VALUE)) {
201+
setOutput(RETURN_VALUE, language.decode(returnValue));
202+
setResolved(RETURN_VALUE, true);
203+
}
204+
200205
// flush output and error streams
201206
if (output != null) {
202207
try {

0 commit comments

Comments
 (0)