Skip to content

Commit 255f10e

Browse files
committed
Cache ScriptModule
The ScriptModule is always passed to the bindings, but this will fail if we aren't running inside MATLAB - as the object can not be passed across JVMs. So let's keep a local copy of it that can at least be used on this side of the JVM.
1 parent 5077b50 commit 255f10e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/main/java/org/scijava/plugins/scripting/matlab/MATLABBindings.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.scijava.log.LogService;
5050
import org.scijava.options.OptionsService;
5151
import org.scijava.plugin.Parameter;
52+
import org.scijava.script.ScriptModule;
5253

5354
/**
5455
* A {@link Bindings} wrapper around MATLAB's local variables.
@@ -73,6 +74,8 @@ public class MATLABBindings implements Bindings {
7374
private final Set<String> keys = new HashSet<String>();
7475
private final Set<Object> values = new HashSet<Object>();
7576
private final Map<String, Object> entries = new HashMap<String, Object>();
77+
private String scriptModuleKey = ScriptModule.class.getName();
78+
private Object scriptModule = null;
7679

7780
// -- Map API --
7881

@@ -108,6 +111,7 @@ public void clear() {
108111
public Set<String> keySet() {
109112
keys.clear();
110113
keys.addAll(Arrays.asList(getVars()));
114+
if (scriptModule != null) keys.add(scriptModuleKey);
111115

112116
return keys;
113117
}
@@ -136,6 +140,12 @@ public Set<java.util.Map.Entry<String, Object>> entrySet() {
136140
public Object put(final String name, final Object value) {
137141
final MatlabProxy proxy = MATLABControlUtils.proxy(opts());
138142

143+
// If we aren't inside MATLAB we cache the ScriptModule in the local JVM
144+
// Because MATLAB is running in a separate JVM we can not pass it a
145+
// ScriptModule instance.
146+
if (!proxy.isRunningInsideMatlab() &&
147+
name.equals(scriptModuleKey)) return (scriptModule = value);
148+
139149
// Try special MATLAB data types
140150
if (value != null) {
141151
MatlabNumericArray arrayVal = null;
@@ -215,6 +225,8 @@ private Object retrieveValue(final Object key, final boolean remove) {
215225
final String k = (String) key;
216226
final MatlabProxy proxy = MATLABControlUtils.proxy(opts());
217227

228+
if (!proxy.isRunningInsideMatlab() && k.equals(scriptModuleKey)) return scriptModule;
229+
218230
Object v = null;
219231
try {
220232
v = proxy.getVariable(k);

0 commit comments

Comments
 (0)