refactor: get rid of InterruptException

This commit is contained in:
TopchetoEU 2024-09-14 18:46:47 +03:00
parent fab3e59910
commit 30674ee463
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 15 additions and 25 deletions

View File

@ -35,7 +35,7 @@ java {
} }
configure([tasks.compileJava]) { configure([tasks.compileJava]) {
options.release = 11 options.release = 8
} }
jar { jar {

View File

@ -1,12 +1,11 @@
package me.topchetoeu.jscript.runtime; package me.topchetoeu.jscript.runtime;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.PriorityBlockingQueue;
import java.util.function.Supplier; import java.util.function.Supplier;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
public final class Engine implements EventLoop { public final class Engine implements EventLoop {
private static class Task<T> implements Comparable<Task<?>> { private static class Task<T> implements Comparable<Task<?>> {
public final Supplier<?> runnable; public final Supplier<?> runnable;
@ -41,10 +40,10 @@ public final class Engine implements EventLoop {
try { try {
((Task<Object>)task).notifier.complete(task.runnable.get()); ((Task<Object>)task).notifier.complete(task.runnable.get());
} }
catch (InterruptException e) { throw e; } catch (CancellationException e) { throw e; }
catch (RuntimeException e) { task.notifier.completeExceptionally(e); } catch (RuntimeException e) { task.notifier.completeExceptionally(e); }
} }
catch (InterruptedException | InterruptException e) { catch (InterruptedException | CancellationException e) {
for (var msg : tasks) msg.notifier.cancel(false); for (var msg : tasks) msg.notifier.cancel(false);
break; break;
} }

View File

@ -2,13 +2,13 @@ package me.topchetoeu.jscript.runtime;
import java.util.Arrays; import java.util.Arrays;
import java.util.Stack; import java.util.Stack;
import java.util.concurrent.CancellationException;
import me.topchetoeu.jscript.common.Instruction; import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.environment.Environment; import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.environment.Key; import me.topchetoeu.jscript.common.environment.Key;
import me.topchetoeu.jscript.runtime.debug.DebugContext; import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException; import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.values.Value; import me.topchetoeu.jscript.runtime.values.Value;
import me.topchetoeu.jscript.runtime.values.functions.CodeFunction; import me.topchetoeu.jscript.runtime.values.functions.CodeFunction;
import me.topchetoeu.jscript.runtime.values.objects.ArrayLikeValue; import me.topchetoeu.jscript.runtime.values.objects.ArrayLikeValue;
@ -176,7 +176,7 @@ public final class Frame {
if (returnValue == null && error == null) { if (returnValue == null && error == null) {
try { try {
if (Thread.interrupted()) throw new InterruptException(); if (Thread.interrupted()) throw new CancellationException();
if (instr == null) { if (instr == null) {
if (stackPtr > 0) returnValue = stack[stackPtr - 1]; if (stackPtr > 0) returnValue = stack[stackPtr - 1];
@ -383,9 +383,9 @@ public final class Frame {
public ObjectValue getValStackScope() { public ObjectValue getValStackScope() {
return new ArrayLikeValue() { return new ArrayLikeValue() {
@Override public Value get(int i) { return stack[i]; } @Override public Value get(int i) { return stack[i]; }
@Override public void set(int i, Value val) { stack[i] = val; } @Override public boolean set(Environment env, int i, Value val) { return false; }
@Override public boolean has(int i) { return i >= 0 && i < size(); } @Override public boolean has(int i) { return i >= 0 && i < size(); }
@Override public void remove(int i) { } @Override public boolean remove(int i) { return false; }
@Override public int size() { return stackPtr; } @Override public int size() { return stackPtr; }
@Override public boolean setSize(int val) { return false; } @Override public boolean setSize(int val) { return false; }

View File

@ -1,8 +1,8 @@
package me.topchetoeu.jscript.runtime; package me.topchetoeu.jscript.runtime;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -13,7 +13,6 @@ import me.topchetoeu.jscript.common.json.JSON;
import me.topchetoeu.jscript.common.parsing.Filename; import me.topchetoeu.jscript.common.parsing.Filename;
import me.topchetoeu.jscript.runtime.debug.DebugContext; import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException; import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException; import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.Member.FieldMember; import me.topchetoeu.jscript.runtime.values.Member.FieldMember;
import me.topchetoeu.jscript.runtime.values.Member.PropertyMember; import me.topchetoeu.jscript.runtime.values.Member.PropertyMember;
@ -45,13 +44,13 @@ public class SimpleRepl {
for (var arg : args) { for (var arg : args) {
try { try {
var file = Path.of(arg); var file = new File(arg);
var raw = Files.readString(file); var raw = Reading.streamToString(new FileInputStream(file));
try { try {
var res = engine.pushMsg( var res = engine.pushMsg(
false, environment, false, environment,
Filename.fromFile(file.toFile()), raw, null Filename.fromFile(file), raw, null
).get(); ).get();
System.err.println(res.toReadable(environment)); System.err.println(res.toReadable(environment));
@ -195,7 +194,7 @@ public class SimpleRepl {
var val = new ArrayValue(); var val = new ArrayValue();
for (var key : args.get(0).getOwnMembers(env, args.get(1).toBoolean())) { for (var key : args.get(0).getOwnMembers(env, args.get(1).toBoolean())) {
val.set(val.size(), new StringValue(key)); val.set(args.env, val.size(), new StringValue(key));
} }
return val; return val;
@ -375,7 +374,7 @@ public class SimpleRepl {
glob.defineOwnMember(null, "exit", new NativeFunction("exit", args -> { glob.defineOwnMember(null, "exit", new NativeFunction("exit", args -> {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new InterruptException(); throw new CancellationException();
})); }));
glob.defineOwnMember(null, "print", new NativeFunction("print", args -> { glob.defineOwnMember(null, "print", new NativeFunction("print", args -> {
for (var el : args.args) { for (var el : args.args) {

View File

@ -1,8 +0,0 @@
package me.topchetoeu.jscript.runtime.exceptions;
public class InterruptException extends RuntimeException {
public InterruptException() { }
public InterruptException(Throwable e) {
super(e);
}
}