Skip to content

Commit 9762eaa

Browse files
committed
Revert "Release script bindings when editor sessions end"
This reverts commit 5c62ee1.
1 parent 5c62ee1 commit 9762eaa

2 files changed

Lines changed: 11 additions & 198 deletions

File tree

‎src/main/java/org/scijava/ui/swing/script/TextEditor.java‎

Lines changed: 11 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@
2929

3030
package org.scijava.ui.swing.script;
3131

32-
import java.awt.BorderLayout;
33-
import java.awt.Color;
34-
import java.awt.Component;
35-
import java.awt.Cursor;
36-
import java.awt.Desktop;
37-
import java.awt.Dimension;
38-
import java.awt.Font;
39-
import java.awt.GridBagConstraints;
40-
import java.awt.GridBagLayout;
41-
import java.awt.Toolkit;
32+
import java.awt.*;
4233
import java.awt.datatransfer.DataFlavor;
4334
import java.awt.datatransfer.Transferable;
4435
import java.awt.datatransfer.UnsupportedFlavorException;
@@ -86,71 +77,38 @@
8677
import java.nio.file.Paths;
8778
import java.nio.file.StandardCopyOption;
8879
import java.nio.file.StandardOpenOption;
89-
import java.time.LocalDateTime;
90-
import java.time.format.DateTimeFormatter;
9180
import java.util.ArrayList;
9281
import java.util.Arrays;
9382
import java.util.Collection;
9483
import java.util.Collections;
9584
import java.util.ConcurrentModificationException;
85+
import java.time.LocalDateTime;
86+
import java.time.format.DateTimeFormatter;
9687
import java.util.Date;
9788
import java.util.Enumeration;
9889
import java.util.HashMap;
9990
import java.util.HashSet;
100-
import java.util.IdentityHashMap;
101-
import java.util.LinkedHashMap;
10291
import java.util.LinkedHashSet;
92+
import java.util.LinkedHashMap;
10393
import java.util.List;
94+
import java.util.concurrent.CopyOnWriteArrayList;
10495
import java.util.Map;
10596
import java.util.Objects;
10697
import java.util.Set;
98+
import java.util.function.BiConsumer;
99+
import java.util.function.IntPredicate;
107100
import java.util.TreeMap;
108101
import java.util.Vector;
109-
import java.util.concurrent.CancellationException;
110-
import java.util.concurrent.CopyOnWriteArrayList;
111102
import java.util.concurrent.ExecutionException;
112-
import java.util.concurrent.Future;
113-
import java.util.function.BiConsumer;
114-
import java.util.function.IntPredicate;
115103
import java.util.jar.JarEntry;
116104
import java.util.jar.JarOutputStream;
117105
import java.util.regex.Matcher;
118106
import java.util.regex.Pattern;
119107
import java.util.zip.ZipException;
120108

121-
import javax.script.Bindings;
122-
import javax.script.ScriptContext;
123109
import javax.script.ScriptEngine;
124110
import javax.script.ScriptException;
125-
import javax.swing.AbstractAction;
126-
import javax.swing.AbstractButton;
127-
import javax.swing.BorderFactory;
128-
import javax.swing.Box;
129-
import javax.swing.BoxLayout;
130-
import javax.swing.ButtonGroup;
131-
import javax.swing.Icon;
132-
import javax.swing.JButton;
133-
import javax.swing.JCheckBoxMenuItem;
134-
import javax.swing.JComponent;
135-
import javax.swing.JFrame;
136-
import javax.swing.JLabel;
137-
import javax.swing.JMenu;
138-
import javax.swing.JMenuBar;
139-
import javax.swing.JMenuItem;
140-
import javax.swing.JOptionPane;
141-
import javax.swing.JPanel;
142-
import javax.swing.JPopupMenu;
143-
import javax.swing.JRadioButtonMenuItem;
144-
import javax.swing.JScrollPane;
145-
import javax.swing.JSplitPane;
146-
import javax.swing.JTabbedPane;
147-
import javax.swing.JTextArea;
148-
import javax.swing.JToggleButton;
149-
import javax.swing.JToolBar;
150-
import javax.swing.KeyStroke;
151-
import javax.swing.SwingUtilities;
152-
import javax.swing.Timer;
153-
import javax.swing.UIManager;
111+
import javax.swing.*;
154112
import javax.swing.event.ChangeEvent;
155113
import javax.swing.event.ChangeListener;
156114
import javax.swing.event.DocumentEvent;
@@ -167,9 +125,9 @@
167125
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
168126
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
169127
import org.fife.ui.rsyntaxtextarea.Theme;
170-
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
171128
import org.fife.ui.rsyntaxtextarea.parser.ParserNotice;
172129
import org.fife.ui.rsyntaxtextarea.parser.TaskTagParser;
130+
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
173131
import org.fife.ui.rtextarea.ClipboardHistory;
174132
import org.fife.ui.rtextarea.Macro;
175133
import org.fife.ui.rtextarea.RTextArea;
@@ -418,9 +376,6 @@ public class TextEditor extends JFrame implements ActionListener,
418376

419377
private EditableScriptInfo scriptInfo;
420378
private ScriptModule module;
421-
private Bindings moduleBindings;
422-
private BindingsRegistry.Lease replBindingsLease;
423-
private static final BindingsRegistry BINDINGS_REGISTRY = new BindingsRegistry();
424379
private boolean incremental = false;
425380
private DragSource dragSource;
426381
private boolean layoutLoading = true;
@@ -3375,10 +3330,6 @@ static void deleteRecursively(final File directory) {
33753330
public void dispose() {
33763331
if (!disposed) { // can be called more than once
33773332
disposed = true;
3378-
releaseReplBindings();
3379-
module = null;
3380-
scriptInfo = null;
3381-
moduleBindings = null;
33823333
layoutSaveTimer.stop();
33833334
titleTimer.stop();
33843335
runTimer.stop();
@@ -3402,9 +3353,6 @@ void setLanguage(final ScriptLanguage language) {
34023353

34033354
void setLanguage(final ScriptLanguage language, final boolean addHeader) {
34043355
if (null != this.getCurrentLanguage() && (null == language || this.getCurrentLanguage().getLanguageName() != language.getLanguageName())) {
3405-
releaseReplBindings();
3406-
this.module = null;
3407-
this.moduleBindings = null;
34083356
this.scriptInfo = null;
34093357
}
34103358
getEditorPane().setLanguage(language, addHeader);
@@ -4702,98 +4650,10 @@ public String getProcessedScript() {
47024650
}
47034651
}
47044652

4705-
static final class BindingsRegistry {
4706-
4707-
private final Map<Bindings, Integer> associationCounts = new IdentityHashMap<>();
4708-
4709-
synchronized Lease retain(final Bindings bindings) {
4710-
if (bindings == null) return new Lease(null, null);
4711-
final int count = associationCounts.getOrDefault(bindings, 0) + 1;
4712-
associationCounts.put(bindings, count);
4713-
return new Lease(this, bindings);
4714-
}
4715-
4716-
private synchronized void release(final Bindings bindings) {
4717-
final Integer count = associationCounts.get(bindings);
4718-
if (count == null) return;
4719-
if (count > 1) associationCounts.put(bindings, count - 1);
4720-
else {
4721-
associationCounts.remove(bindings);
4722-
bindings.clear();
4723-
}
4724-
}
4725-
4726-
static final class Lease implements AutoCloseable {
4727-
4728-
private BindingsRegistry registry;
4729-
private Bindings bindings;
4730-
4731-
private Lease(final BindingsRegistry registry, final Bindings bindings) {
4732-
this.registry = registry;
4733-
this.bindings = bindings;
4734-
}
4735-
4736-
@Override
4737-
public synchronized void close() {
4738-
if (registry == null) return;
4739-
final BindingsRegistry owner = registry;
4740-
final Bindings releasedBindings = bindings;
4741-
registry = null;
4742-
bindings = null;
4743-
owner.release(releasedBindings);
4744-
}
4745-
}
4746-
}
4747-
4748-
private Bindings engineBindings(final ScriptModule module) {
4749-
final ScriptEngine engine = module == null ? null : module.getEngine();
4750-
return engine == null ? null : engine.getBindings(ScriptContext.ENGINE_SCOPE);
4751-
}
4752-
4753-
private synchronized void retainReplBindings(final Bindings bindings) {
4754-
releaseReplBindings();
4755-
if (!disposed) replBindingsLease = BINDINGS_REGISTRY.retain(bindings);
4756-
}
4757-
4758-
private synchronized void releaseReplBindings() {
4759-
if (replBindingsLease == null) return;
4760-
replBindingsLease.close();
4761-
replBindingsLease = null;
4762-
}
4763-
4764-
private static void releaseBindingsWhenComplete(final Future<?> future,
4765-
final BindingsRegistry.Lease lease)
4766-
{
4767-
final Thread cleanupThread = new Thread(() -> {
4768-
boolean interrupted = false;
4769-
try {
4770-
while (true) {
4771-
try {
4772-
future.get();
4773-
return;
4774-
}
4775-
catch (final InterruptedException e) {
4776-
interrupted = true;
4777-
}
4778-
catch (final ExecutionException | CancellationException e) {
4779-
return;
4780-
}
4781-
}
4782-
}
4783-
finally {
4784-
lease.close();
4785-
if (interrupted) Thread.currentThread().interrupt();
4786-
}
4787-
}, "script-editor-bindings-release");
4788-
cleanupThread.setDaemon(true);
4789-
cleanupThread.start();
4790-
}
4791-
47924653
private Reader evalScript(final String filename, Reader reader,
47934654
final Writer output, final JTextAreaWriter errors) throws ModuleException
47944655
{
47954656
final ScriptLanguage language = getCurrentLanguage();
4796-
final boolean preserveState = incremental && !language.isCompiledLanguage();
47974657

47984658
// If there's no engine or the language has changed or the language is compiled from a file
47994659
// then create the engine and module anew
@@ -4805,8 +4665,6 @@ private Reader evalScript(final String filename, Reader reader,
48054665
&& this.scriptInfo.getLanguage().getLanguageName()
48064666
!= getCurrentLanguage().getLanguageName()))
48074667
{
4808-
releaseReplBindings();
4809-
this.moduleBindings = null;
48104668
if (respectAutoImports) {
48114669
reader =
48124670
DefaultAutoImporters.prefixAutoImports(context, language, reader,
@@ -4820,8 +4678,6 @@ private Reader evalScript(final String filename, Reader reader,
48204678

48214679
this.module = this.scriptInfo.createModule();
48224680
context.inject(this.module);
4823-
this.moduleBindings = engineBindings(this.module);
4824-
if (preserveState) retainReplBindings(this.moduleBindings);
48254681
} else {
48264682
try {
48274683
// Same engine, with persistent state
@@ -4833,7 +4689,6 @@ private Reader evalScript(final String filename, Reader reader,
48334689

48344690
// map stdout and stderr to the UI
48354691
final ScriptModule runModule = this.module;
4836-
final BindingsRegistry.Lease runBindingsLease = BINDINGS_REGISTRY.retain(moduleBindings);
48374692
runModule.setOutputWriter(output);
48384693
runModule.setErrorWriter(errors);
48394694

@@ -4843,10 +4698,8 @@ private Reader evalScript(final String filename, Reader reader,
48434698
getEditorPane().getErrorHighlighter().setWriter(errors);
48444699

48454700
// execute the script
4846-
Future<?> runFuture = null;
48474701
try {
4848-
runFuture = moduleService.run(runModule, true);
4849-
runFuture.get();
4702+
moduleService.run(runModule, true).get();
48504703
}
48514704
catch (final InterruptedException e) {
48524705
error("Interrupted");
@@ -4857,19 +4710,13 @@ private Reader evalScript(final String filename, Reader reader,
48574710
getEditorPane().getErrorHighlighter().parse(e);
48584711
} finally {
48594712
getEditorPane().getErrorHighlighter().parse();
4860-
if (runFuture != null && !runFuture.isDone()) {
4861-
releaseBindingsWhenComplete(runFuture, runBindingsLease);
4862-
}
4863-
else runBindingsLease.close();
48644713
// A one-off run must not keep its module: it holds the script engine and thus every
48654714
// variable the script created (e.g., large images), even after this window is closed.
48664715
// Objects the script itself keeps alive (threads, listeners, displays) are not
48674716
// affected. In REPL mode the state is kept on purpose
4868-
if ((!preserveState || disposed) && this.module == runModule) {
4869-
releaseReplBindings();
4717+
if (!incremental && this.module == runModule) {
48704718
this.module = null;
48714719
this.scriptInfo = null;
4872-
this.moduleBindings = null;
48734720
}
48744721
}
48754722
return reader;
@@ -4882,12 +4729,6 @@ public void setIncremental(final boolean incremental) {
48824729
return;
48834730
}
48844731

4885-
if (!incremental && this.incremental) {
4886-
releaseReplBindings();
4887-
this.module = null;
4888-
this.scriptInfo = null;
4889-
this.moduleBindings = null;
4890-
}
48914732
this.incremental = incremental;
48924733

48934734
final JTextArea prompt = getTab().getPrompt();

‎src/test/java/org/scijava/ui/swing/script/TextEditorThreadOwnershipTest.java‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,17 @@
2929
package org.scijava.ui.swing.script;
3030

3131
import static org.junit.Assert.assertFalse;
32-
import static org.junit.Assert.assertEquals;
3332
import static org.junit.Assert.assertNotNull;
3433
import static org.junit.Assert.assertNull;
3534
import static org.junit.Assert.assertSame;
3635
import static org.junit.Assert.assertTrue;
3736

3837
import java.lang.reflect.Modifier;
39-
import java.util.HashMap;
4038
import java.util.concurrent.CountDownLatch;
4139
import java.util.concurrent.Future;
4240
import java.util.concurrent.TimeUnit;
4341
import java.util.concurrent.atomic.AtomicReference;
4442

45-
import javax.script.Bindings;
46-
import javax.script.SimpleBindings;
47-
4843
import org.junit.After;
4944
import org.junit.Before;
5045
import org.junit.Test;
@@ -73,29 +68,6 @@ public void testTitleSetterDoesNotSynchronizeOnEditor() throws Exception {
7368
String.class).getModifiers()));
7469
}
7570

76-
@Test
77-
public void testBindingsAreClearedAfterLastAssociation() {
78-
final TextEditor.BindingsRegistry registry = new TextEditor.BindingsRegistry();
79-
final Object image = new Object();
80-
final Bindings first = new SimpleBindings();
81-
first.put("image", image);
82-
final Bindings second = new SimpleBindings();
83-
second.put("image", image);
84-
assertEquals(new HashMap<>(first), new HashMap<>(second));
85-
86-
final TextEditor.BindingsRegistry.Lease firstLease = registry.retain(first);
87-
final TextEditor.BindingsRegistry.Lease secondLease = registry.retain(first);
88-
final TextEditor.BindingsRegistry.Lease distinctLease = registry.retain(second);
89-
90-
firstLease.close();
91-
assertSame(image, first.get("image"));
92-
secondLease.close();
93-
assertTrue(first.isEmpty());
94-
assertSame(image, second.get("image"));
95-
distinctLease.close();
96-
assertTrue(second.isEmpty());
97-
}
98-
9971
@Test
10072
public void testOrdinaryChildThreadBelongsToRun() {
10173
final ThreadGroup runGroup = new ThreadGroup("script-run");

0 commit comments

Comments
 (0)