|
34 | 34 | import java.io.BufferedReader; |
35 | 35 | import java.io.Reader; |
36 | 36 | import java.io.StringReader; |
| 37 | +import java.util.Random; |
37 | 38 |
|
38 | 39 | import javax.script.ScriptException; |
39 | 40 |
|
@@ -98,45 +99,44 @@ public Object eval(final Reader reader) throws ScriptException { |
98 | 99 | final MATLABOptions options = |
99 | 100 | optionsService.getOptions(MATLABOptions.class); |
100 | 101 | final MatlabProxy proxy = MATLABControlUtils.proxy(options); |
101 | | - final Thread thread = Thread.currentThread(); |
102 | 102 | Object finalResult = null; |
103 | 103 | try { |
104 | | - while (!thread.isInterrupted()) { |
105 | | - String command = "sprintf('"; |
106 | | - while (bufReader.ready()) { |
107 | | - final String line = bufReader.readLine(); |
108 | | - if (line == null) break; |
109 | | - |
110 | | - // NB: we have to manually exclude comment lines in MATLAB. Otherwise, |
111 | | - // the newline characters themselves on the comment lines will be |
112 | | - // commented out and ignored - resulting in the first true line of |
113 | | - // code being skipped unintentionally. |
114 | | - if (line.matches("^[^\\w]*" + COMMENT + ".*")) continue; |
115 | | - command += line + "\\n"; |
116 | | - } |
117 | | - command += "')"; |
118 | | - |
119 | | - // Evaluate the command |
120 | | - // NB: the first eval turns a multi-line command into something properly |
121 | | - // formatted for MATLAB, stored in the MATLAB variable "ans". |
122 | | - // We then have to evaluate "ans". However, the eval methods of |
123 | | - // MatlabControl force "eval(' + args + ')" and "eval('ans')" displays |
124 | | - // the string literal "ans", whereas "eval(ans)" actually evaluates |
125 | | - // whatever is stored in ans. We wan the latter behavior, thus the |
126 | | - // nested eval. |
127 | | - proxy.eval(command); |
128 | | - |
129 | | - try { |
130 | | - // Attempt to get a return value |
131 | | - finalResult = proxy.returningEval("eval(ans)", 1); |
132 | | - } |
133 | | - catch (final MatlabInvocationException e) { |
134 | | - // no return value. Just eval and be done. |
135 | | - // NB: modified MATLAB variables can be accessed via the bindings. |
136 | | - proxy.eval("eval(ans)"); |
137 | | - } |
138 | | - break; |
| 104 | + final String scriptVar = "scijava_script" + new Random().nextInt(999999); |
| 105 | + String command = scriptVar + " = sprintf('"; |
| 106 | + while (bufReader.ready()) { |
| 107 | + final String line = bufReader.readLine(); |
| 108 | + if (line == null) break; |
| 109 | + |
| 110 | + // NB: we have to manually exclude comment lines in MATLAB. Otherwise, |
| 111 | + // the newline characters themselves on the comment lines will be |
| 112 | + // commented out and ignored - resulting in the first true line of |
| 113 | + // code being skipped unintentionally. |
| 114 | + if (line.matches("^[^\\w]*" + COMMENT + ".*")) continue; |
| 115 | + command += line + "\\n"; |
139 | 116 | } |
| 117 | + command += "')"; |
| 118 | + |
| 119 | + // NB: this first eval turns a multi-line command into something properly |
| 120 | + // formatted for MATLAB, stored in a temporary MATLAB variable |
| 121 | + // We then have to evaluate this variable. However, the eval methods of |
| 122 | + // MatlabControl force "eval(' + args + ')" and "eval('var')" displays |
| 123 | + // the string literal "var", whereas "eval(var)" actually evaluates |
| 124 | + // whatever is stored in var. We want the latter behavior, thus the |
| 125 | + // need for a nested eval. |
| 126 | + proxy.eval(command); |
| 127 | + |
| 128 | + // Perform nested eval.. with or without a return value |
| 129 | + try { |
| 130 | + // Attempt to get a return value |
| 131 | + finalResult = proxy.returningEval("eval(" + scriptVar + ")", 1); |
| 132 | + } |
| 133 | + catch (final MatlabInvocationException e) { |
| 134 | + // no return value. Just eval and be done. |
| 135 | + // NB: modified MATLAB variables can be accessed via the bindings. |
| 136 | + proxy.eval("eval(" + scriptVar + ")"); |
| 137 | + } |
| 138 | + |
| 139 | + proxy.eval("clearvars " + scriptVar); |
140 | 140 | } |
141 | 141 | catch (final Exception e) {} |
142 | 142 |
|
|
0 commit comments