Skip to content

Commit

Permalink
ensure safety
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Apr 7, 2021
1 parent d8a22b5 commit 81be41c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public JEPLanguageDefinition(String extension, Core runner) {
}

protected void execContext(ContextContainer<SharedInterpreter> ctx, Executor exec) throws Exception {
BlockingQueue<Runnable> taskQueue = ((JEPScriptContext) ctx.getCtx()).taskQueue;
try (SharedInterpreter interp = new SharedInterpreter()) {
ctx.getCtx().setContext(interp);

BlockingQueue<Runnable> taskQueue = ((JEPScriptContext) ctx.getCtx()).taskQueue;
for (Map.Entry<String, BaseLibrary> lib : retrieveLibs(ctx).entrySet()) interp.set(lib.getKey(), lib.getValue());

exec.accept(interp);
Expand All @@ -40,6 +40,7 @@ protected void execContext(ContextContainer<SharedInterpreter> ctx, Executor exe
}
} catch (InterruptedException ignored) {}
}
taskQueue.forEach(Runnable::run);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,18 +13,18 @@ public class JEPScriptContext extends ScriptContext<SharedInterpreter> {
public boolean doLoop = false;
public final BlockingQueue<Runnable> 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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
@Library(value = "JavaWrapper", languages = JEPLanguageDefinition.class)
public class FWrapper extends PerExecLanguageLibrary<SharedInterpreter> implements IFWrapper<PyCallable> {
private boolean first = true;
private final Thread t;

public FWrapper(ContextContainer<SharedInterpreter> context, Class<? extends BaseLanguage<SharedInterpreter>> language) {
super(context, language);
t = context.getLockThread();
}

@Override
Expand All @@ -33,17 +35,21 @@ public <A, B, R> MethodWrapper<A, B, R> methodToJava(PyCallable c) {
public R get() {
Synchronizer s = new Synchronizer();
AtomicReference<R> retval = new AtomicReference<>();
AtomicReference<Throwable> 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();
}
Expand All @@ -59,17 +65,21 @@ public int compare(A o1, A o2) {
@Override
public void run() {
Synchronizer s = new Synchronizer();
AtomicReference<Throwable> 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();
}
Expand All @@ -78,17 +88,21 @@ public void run() {
@Override
public void accept(A a) {
Synchronizer s = new Synchronizer();
AtomicReference<Throwable> 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();
}
Expand All @@ -97,17 +111,21 @@ public void accept(A a) {
@Override
public void accept(A a, B b) {
Synchronizer s = new Synchronizer();
AtomicReference<Throwable> 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();
}
Expand All @@ -117,17 +135,21 @@ public void accept(A a, B b) {
public R apply(A a) {
Synchronizer s = new Synchronizer();
AtomicReference<R> retval = new AtomicReference<>();
AtomicReference<Throwable> 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();
}
Expand All @@ -139,17 +161,21 @@ public R apply(A a) {
public R apply(A a, B b) {
Synchronizer s = new Synchronizer();
AtomicReference<R> retval = new AtomicReference<>();
AtomicReference<Throwable> 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();
}
Expand All @@ -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;
}
};
}

Expand All @@ -181,17 +212,21 @@ public <A, B, R> MethodWrapper<A, B, R> methodToJavaAsync(PyCallable c) {
public R get() {
Synchronizer s = new Synchronizer();
AtomicReference<R> retval = new AtomicReference<>();
AtomicReference<Throwable> 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();
}
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -253,17 +291,21 @@ public void accept(A a, B b) {
public R apply(A a) {
Synchronizer s = new Synchronizer();
AtomicReference<R> retval = new AtomicReference<>();
AtomicReference<Throwable> 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();
}
Expand All @@ -275,17 +317,20 @@ public R apply(A a) {
public R apply(A a, B b) {
Synchronizer s = new Synchronizer();
AtomicReference<R> retval = new AtomicReference<>();

AtomicReference<Throwable> 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();
}
Expand All @@ -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;
}
};
}

Expand Down

0 comments on commit 81be41c

Please sign in to comment.