diff --git a/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPLanguageDefinition.java b/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPLanguageDefinition.java index eb6296c..bf15b46 100644 --- a/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPLanguageDefinition.java +++ b/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPLanguageDefinition.java @@ -24,10 +24,10 @@ public JEPLanguageDefinition(String extension, Core runner) { } protected void execContext(ContextContainer ctx, Executor exec) throws Exception { + BlockingQueue taskQueue = ((JEPScriptContext) ctx.getCtx()).taskQueue; try (SharedInterpreter interp = new SharedInterpreter()) { ctx.getCtx().setContext(interp); - BlockingQueue taskQueue = ((JEPScriptContext) ctx.getCtx()).taskQueue; for (Map.Entry lib : retrieveLibs(ctx).entrySet()) interp.set(lib.getKey(), lib.getValue()); exec.accept(interp); @@ -40,6 +40,7 @@ protected void execContext(ContextContainer ctx, Executor exe } } catch (InterruptedException ignored) {} } + taskQueue.forEach(Runnable::run); } @Override diff --git a/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPScriptContext.java b/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPScriptContext.java index b52215d..a37c04a 100644 --- a/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPScriptContext.java +++ b/src/main/java/xyz/wagyourtail/jsmacros/jep/language/impl/JEPScriptContext.java @@ -2,6 +2,7 @@ import jep.JepException; import jep.SharedInterpreter; +import xyz.wagyourtail.jsmacros.core.Core; import xyz.wagyourtail.jsmacros.core.language.ScriptContext; import java.util.concurrent.BlockingQueue; @@ -12,18 +13,18 @@ public class JEPScriptContext extends ScriptContext { public boolean doLoop = false; public final BlockingQueue taskQueue = new LinkedBlockingQueue<>(); + @Override + public boolean isContextClosed() { + return super.isContextClosed() || closed; + } + @Override public void closeContext() { if (this.context != null) { SharedInterpreter ctx = context.get(); if (ctx != null) { - try { - ctx.close(); - closed = true; - taskQueue.put(() -> {}); - } catch (JepException | InterruptedException e) { - e.printStackTrace(); - } + closed = true; + Core.instance.threadContext.entrySet().stream().filter(e -> e.getValue() == this).forEach(e -> e.getKey().interrupt()); } } } diff --git a/src/main/java/xyz/wagyourtail/jsmacros/jep/library/impl/FWrapper.java b/src/main/java/xyz/wagyourtail/jsmacros/jep/library/impl/FWrapper.java index 2052ca3..ae80d99 100644 --- a/src/main/java/xyz/wagyourtail/jsmacros/jep/library/impl/FWrapper.java +++ b/src/main/java/xyz/wagyourtail/jsmacros/jep/library/impl/FWrapper.java @@ -16,9 +16,11 @@ @Library(value = "JavaWrapper", languages = JEPLanguageDefinition.class) public class FWrapper extends PerExecLanguageLibrary implements IFWrapper { private boolean first = true; + private final Thread t; public FWrapper(ContextContainer context, Class> language) { super(context, language); + t = context.getLockThread(); } @Override @@ -33,17 +35,21 @@ public MethodWrapper methodToJava(PyCallable c) { public R get() { Synchronizer s = new Synchronizer(); AtomicReference retval = new AtomicReference<>(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { retval.set((R) c.call()); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -59,17 +65,21 @@ public int compare(A o1, A o2) { @Override public void run() { Synchronizer s = new Synchronizer(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { c.call(); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -78,17 +88,21 @@ public void run() { @Override public void accept(A a) { Synchronizer s = new Synchronizer(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { c.call(a); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -97,17 +111,21 @@ public void accept(A a) { @Override public void accept(A a, B b) { Synchronizer s = new Synchronizer(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { c.call(a, b); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -117,17 +135,21 @@ public void accept(A a, B b) { public R apply(A a) { Synchronizer s = new Synchronizer(); AtomicReference retval = new AtomicReference<>(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { retval.set((R) c.call(a)); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -139,17 +161,21 @@ public R apply(A a) { public R apply(A a, B b) { Synchronizer s = new Synchronizer(); AtomicReference retval = new AtomicReference<>(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { retval.set((R) c.call(a, b)); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -166,6 +192,11 @@ public boolean test(A a) { public boolean test(A a, B b) { return (Boolean) apply(a, b); } + + @Override + public Thread overrideThread() { + return t; + } }; } @@ -181,17 +212,21 @@ public MethodWrapper methodToJavaAsync(PyCallable c) { public R get() { Synchronizer s = new Synchronizer(); AtomicReference retval = new AtomicReference<>(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { retval.set((R) c.call()); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -207,6 +242,7 @@ public int compare(A o1, A o2) { @Override public void run() { try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { c.call(); @@ -222,6 +258,7 @@ public void run() { @Override public void accept(A a) { try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { c.call(a); @@ -237,6 +274,7 @@ public void accept(A a) { @Override public void accept(A a, B b) { try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { c.call(a, b); @@ -253,17 +291,21 @@ public void accept(A a, B b) { public R apply(A a) { Synchronizer s = new Synchronizer(); AtomicReference retval = new AtomicReference<>(); + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { retval.set((R) c.call(a)); } catch (Exception e) { - e.printStackTrace(); + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -275,17 +317,20 @@ public R apply(A a) { public R apply(A a, B b) { Synchronizer s = new Synchronizer(); AtomicReference retval = new AtomicReference<>(); - + AtomicReference ev = new AtomicReference<>(); try { + if (ctx.getCtx().isContextClosed()) throw new RuntimeException("Context Closed"); ((JEPScriptContext)ctx.getCtx()).taskQueue.put(() -> { try { retval.set((R) c.call(a, b)); - } catch (Exception e) { - e.printStackTrace(); + } catch (Throwable e) { + ev.set(e); + } finally { + s.gainOwnershipAndNotifyAll(); } - s.gainOwnershipAndNotifyAll(); }); s.gainOwnershipAndWait(); + if (ev.get() != null) throw new RuntimeException(ev.get()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -302,6 +347,11 @@ public boolean test(A a) { public boolean test(A a, B b) { return (Boolean) apply(a, b); } + + @Override + public Thread overrideThread() { + return t; + } }; }