refactor: remove old useless exceptions

This commit is contained in:
TopchetoEU 2024-01-06 19:51:48 +02:00
parent 18d22a1282
commit 89eea7d62b
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
10 changed files with 263 additions and 267 deletions

View File

@ -125,7 +125,7 @@ public class Main {
engineTask = engine.start();
debugTask = debugServer.start(new InetSocketAddress("127.0.0.1", 9229), true);
}
private static void initTypescript() {
private static void initTypescript() throws IOException {
var tsEnv = Internals.apply(new Environment());
var bsEnv = Internals.apply(new Environment());

View File

@ -4,8 +4,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import me.topchetoeu.jscript.exceptions.UncheckedException;
import java.io.UncheckedIOException;
public class Reading {
private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
@ -14,9 +13,16 @@ public class Reading {
return reader.readLine();
}
/**
* Reads the given stream to a string
* @param in
* @return
*/
public static String streamToString(InputStream in) {
try { return new String(in.readAllBytes()); }
catch (Throwable e) { throw new UncheckedException(e); }
try {
return new String(in.readAllBytes());
}
catch (IOException e) { throw new UncheckedIOException(e); }
}
public static InputStream resourceToStream(String name) {
return Reading.class.getResourceAsStream("/" + name);

View File

@ -1,31 +1,33 @@
package me.topchetoeu.jscript.engine.debug;
import java.io.IOException;
public interface DebugHandler {
void enable(V8Message msg);
void disable(V8Message msg);
void enable(V8Message msg) throws IOException;
void disable(V8Message msg) throws IOException;
void setBreakpointByUrl(V8Message msg);
void removeBreakpoint(V8Message msg);
void continueToLocation(V8Message msg);
void setBreakpointByUrl(V8Message msg) throws IOException;
void removeBreakpoint(V8Message msg) throws IOException;
void continueToLocation(V8Message msg) throws IOException;
void getScriptSource(V8Message msg);
void getPossibleBreakpoints(V8Message msg);
void getScriptSource(V8Message msg) throws IOException;
void getPossibleBreakpoints(V8Message msg) throws IOException;
void resume(V8Message msg);
void pause(V8Message msg);
void resume(V8Message msg) throws IOException;
void pause(V8Message msg) throws IOException;
void stepInto(V8Message msg);
void stepOut(V8Message msg);
void stepOver(V8Message msg);
void stepInto(V8Message msg) throws IOException;
void stepOut(V8Message msg) throws IOException;
void stepOver(V8Message msg) throws IOException;
void setPauseOnExceptions(V8Message msg);
void setPauseOnExceptions(V8Message msg) throws IOException;
void evaluateOnCallFrame(V8Message msg);
void evaluateOnCallFrame(V8Message msg) throws IOException;
void getProperties(V8Message msg);
void releaseObjectGroup(V8Message msg);
void releaseObject(V8Message msg);
void callFunctionOn(V8Message msg);
void getProperties(V8Message msg) throws IOException;
void releaseObjectGroup(V8Message msg) throws IOException;
void releaseObject(V8Message msg) throws IOException;
void callFunctionOn(V8Message msg) throws IOException;
void runtimeEnable(V8Message msg);
void runtimeEnable(V8Message msg) throws IOException;
}

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.engine.debug;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
@ -13,8 +14,6 @@ import me.topchetoeu.jscript.Reading;
import me.topchetoeu.jscript.engine.debug.WebSocketMessage.Type;
import me.topchetoeu.jscript.events.Notifier;
import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.exceptions.UncheckedException;
import me.topchetoeu.jscript.exceptions.UncheckedIOException;
import me.topchetoeu.jscript.json.JSON;
import me.topchetoeu.jscript.json.JSONList;
import me.topchetoeu.jscript.json.JSONMap;
@ -36,8 +35,7 @@ public class DebugServer {
try {
return MessageDigest.getInstance("sha1");
}
catch (Throwable e) { throw new UncheckedException(e); }
catch (Throwable e) { throw new RuntimeException(e); }
}
private static Thread runAsync(Runnable func, String name) {
@ -47,7 +45,7 @@ public class DebugServer {
return res;
}
private void handle(WebSocket ws, Debugger debugger) {
private void handle(WebSocket ws, Debugger debugger) throws IOException {
WebSocketMessage raw;
while ((raw = ws.receive()) != null) {
@ -66,7 +64,6 @@ public class DebugServer {
return;
}
try {
switch (msg.name) {
case "Debugger.enable":
connNotifier.next();
@ -109,11 +106,6 @@ public class DebugServer {
) ws.send(new V8Error("This isn't a browser..."));
else ws.send(new V8Error("This API is not supported yet."));
}
catch (Throwable e) {
e.printStackTrace();
throw new UncheckedException(e);
}
}
debugger.close();
}
@ -147,9 +139,12 @@ public class DebugServer {
runAsync(() -> {
try { handle(ws, debugger); }
catch (RuntimeException e) {
catch (RuntimeException | IOException e) {
try {
ws.send(new V8Error(e.getMessage()));
}
catch (IOException e2) { /* Shit outta luck */ }
}
finally { ws.close(); debugger.close(); }
}, "Debug Handler");
}
@ -239,6 +234,8 @@ public class DebugServer {
.replace("${AUTHOR}", Metadata.author())
.getBytes();
}
catch (IOException e) { throw new UncheckedIOException(e); }
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@ -1,5 +1,6 @@
package me.topchetoeu.jscript.engine.debug;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -453,11 +454,18 @@ public class SimpleDebugger implements Debugger {
}
private void resume(State state) {
try {
this.state = state;
ws.send(new V8Event("Debugger.resumed", new JSONMap()));
updateNotifier.next();
}
catch (IOException e) {
ws.close();
close();
}
}
private void pauseDebug(Context ctx, Breakpoint bp) {
try {
state = State.PAUSED_NORMAL;
var map = new JSONMap()
.set("callFrames", serializeFrames(ctx))
@ -466,7 +474,13 @@ public class SimpleDebugger implements Debugger {
if (bp != null) map.set("hitBreakpoints", new JSONList().add(bp.id + ""));
ws.send(new V8Event("Debugger.paused", map));
}
catch (IOException e) {
ws.close();
close();
}
}
private void pauseException(Context ctx) {
try {
state = State.PAUSED_EXCEPTION;
var map = new JSONMap()
.set("callFrames", serializeFrames(ctx))
@ -474,16 +488,28 @@ public class SimpleDebugger implements Debugger {
ws.send(new V8Event("Debugger.paused", map));
}
catch (IOException e) {
ws.close();
close();
}
}
private void sendSource(Source src) {
private void sendSource(Source src){
try {
ws.send(new V8Event("Debugger.scriptParsed", new JSONMap()
.set("scriptId", src.id + "")
.set("hash", src.source.hashCode())
.set("url", src.filename + "")
));
}
catch (IOException e) {
ws.close();
close();
}
}
private void addBreakpoint(Breakpoint bpt) {
try {
idToBreakpoint.put(bpt.id, bpt);
locToBreakpoint.put(bpt.location, bpt);
@ -492,6 +518,11 @@ public class SimpleDebugger implements Debugger {
.set("location", serializeLocation(bpt.location))
));
}
catch (IOException e) {
ws.close();
close();
}
}
private RunResult run(Frame codeFrame, String code) {
if (codeFrame == null) return new RunResult(null, code, new EngineException("Invalid code frame!"));
@ -582,7 +613,7 @@ public class SimpleDebugger implements Debugger {
return resObj;
}
@Override public synchronized void enable(V8Message msg) {
@Override public synchronized void enable(V8Message msg) throws IOException {
enabled = true;
ws.send(msg.respond());
@ -591,7 +622,7 @@ public class SimpleDebugger implements Debugger {
updateNotifier.next();
}
@Override public synchronized void disable(V8Message msg) {
@Override public synchronized void disable(V8Message msg) throws IOException {
close();
ws.send(msg.respond());
}
@ -625,11 +656,11 @@ public class SimpleDebugger implements Debugger {
updateNotifier.next();
}
@Override public synchronized void getScriptSource(V8Message msg) {
@Override public synchronized void getScriptSource(V8Message msg) throws IOException {
int id = Integer.parseInt(msg.params.string("scriptId"));
ws.send(msg.respond(new JSONMap().set("scriptSource", idToSource.get(id).source)));
}
@Override public synchronized void getPossibleBreakpoints(V8Message msg) {
@Override public synchronized void getPossibleBreakpoints(V8Message msg) throws IOException {
var src = idToSource.get(Integer.parseInt(msg.params.map("start").string("scriptId")));
var start = deserializeLocation(msg.params.get("start"), false);
var end = msg.params.isMap("end") ? deserializeLocation(msg.params.get("end"), false) : null;
@ -644,16 +675,16 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond(new JSONMap().set("locations", res)));
}
@Override public synchronized void pause(V8Message msg) {
@Override public synchronized void pause(V8Message msg) throws IOException {
pendingPause = true;
ws.send(msg.respond());
}
@Override public synchronized void resume(V8Message msg) {
@Override public synchronized void resume(V8Message msg) throws IOException {
resume(State.RESUMED);
ws.send(msg.respond(new JSONMap()));
}
@Override public synchronized void setBreakpointByUrl(V8Message msg) {
@Override public synchronized void setBreakpointByUrl(V8Message msg) throws IOException {
var line = (int)msg.params.number("lineNumber") + 1;
var col = (int)msg.params.number("columnNumber", 0) + 1;
var cond = msg.params.string("condition", "").trim();
@ -688,7 +719,7 @@ public class SimpleDebugger implements Debugger {
.set("locations", locs)
));
}
@Override public synchronized void removeBreakpoint(V8Message msg) {
@Override public synchronized void removeBreakpoint(V8Message msg) throws IOException {
var id = Integer.parseInt(msg.params.string("breakpointId"));
if (idToBptCand.containsKey(id)) {
@ -705,7 +736,7 @@ public class SimpleDebugger implements Debugger {
}
ws.send(msg.respond());
}
@Override public synchronized void continueToLocation(V8Message msg) {
@Override public synchronized void continueToLocation(V8Message msg) throws IOException {
var loc = deserializeLocation(msg.params.get("location"), true);
tmpBreakpts.add(loc);
@ -714,7 +745,7 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond());
}
@Override public synchronized void setPauseOnExceptions(V8Message msg) {
@Override public synchronized void setPauseOnExceptions(V8Message msg) throws IOException {
switch (msg.params.string("state")) {
case "none": execptionType = CatchType.NONE; break;
case "all": execptionType = CatchType.ALL; break;
@ -727,7 +758,7 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond());
}
@Override public synchronized void stepInto(V8Message msg) {
@Override public synchronized void stepInto(V8Message msg) throws IOException {
if (state == State.RESUMED) ws.send(new V8Error("Debugger is resumed."));
else {
stepOutFrame = currFrame;
@ -736,7 +767,7 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond());
}
}
@Override public synchronized void stepOut(V8Message msg) {
@Override public synchronized void stepOut(V8Message msg) throws IOException {
if (state == State.RESUMED) ws.send(new V8Error("Debugger is resumed."));
else {
stepOutFrame = currFrame;
@ -745,7 +776,7 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond());
}
}
@Override public synchronized void stepOver(V8Message msg) {
@Override public synchronized void stepOver(V8Message msg) throws IOException {
if (state == State.RESUMED) ws.send(new V8Error("Debugger is resumed."));
else {
stepOutFrame = currFrame;
@ -755,7 +786,7 @@ public class SimpleDebugger implements Debugger {
}
}
@Override public synchronized void evaluateOnCallFrame(V8Message msg) {
@Override public synchronized void evaluateOnCallFrame(V8Message msg) throws IOException {
var cfId = Integer.parseInt(msg.params.string("callFrameId"));
var expr = msg.params.string("expression");
var group = msg.params.string("objectGroup", null);
@ -769,12 +800,12 @@ public class SimpleDebugger implements Debugger {
else ws.send(msg.respond(new JSONMap().set("result", serializeObj(res.ctx, res.result))));
}
@Override public synchronized void releaseObjectGroup(V8Message msg) {
@Override public synchronized void releaseObjectGroup(V8Message msg) throws IOException {
var group = msg.params.string("objectGroup");
releaseGroup(group);
ws.send(msg.respond());
}
@Override public synchronized void releaseObject(V8Message msg) {
@Override public synchronized void releaseObject(V8Message msg) throws IOException {
var id = Integer.parseInt(msg.params.string("objectId"));
var ref = idToObject.get(id);
ref.held = false;
@ -786,7 +817,7 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond());
}
@Override public synchronized void getProperties(V8Message msg) {
@Override public synchronized void getProperties(V8Message msg) throws IOException {
var ref = idToObject.get(Integer.parseInt(msg.params.string("objectId")));
var obj = ref.obj;
@ -833,7 +864,7 @@ public class SimpleDebugger implements Debugger {
ws.send(msg.respond(new JSONMap().set("result", res)));
}
@Override public synchronized void callFunctionOn(V8Message msg) {
@Override public synchronized void callFunctionOn(V8Message msg) throws IOException {
var src = msg.params.string("functionDeclaration");
var args = msg.params
.list("arguments", new JSONList())
@ -881,7 +912,7 @@ public class SimpleDebugger implements Debugger {
catch (EngineException e) { ws.send(msg.respond(new JSONMap().set("exceptionDetails", serializeException(ctx, e)))); }
}
@Override public synchronized void runtimeEnable(V8Message msg) {
@Override public synchronized void runtimeEnable(V8Message msg) throws IOException {
ws.send(msg.respond());
}
@ -921,7 +952,11 @@ public class SimpleDebugger implements Debugger {
if (error != null && (execptionType == CatchType.ALL || execptionType == CatchType.UNCAUGHT && !caught)) {
pauseException(ctx);
}
else if (loc != null && (state == State.STEPPING_IN || state == State.STEPPING_OVER) && returnVal != Values.NO_RETURN && stepOutFrame == frame) {
else if (
loc != null &&
(state == State.STEPPING_IN || state == State.STEPPING_OVER) &&
returnVal != Values.NO_RETURN && stepOutFrame == frame
) {
pauseDebug(ctx, null);
}
else if (isBreakpointable && locToBreakpoint.containsKey(loc)) {

View File

@ -7,7 +7,6 @@ import java.io.OutputStream;
import java.net.Socket;
import me.topchetoeu.jscript.engine.debug.WebSocketMessage.Type;
import me.topchetoeu.jscript.exceptions.UncheckedIOException;
public class WebSocket implements AutoCloseable {
public long maxLength = 1 << 20;
@ -15,19 +14,16 @@ public class WebSocket implements AutoCloseable {
private Socket socket;
private boolean closed = false;
private OutputStream out() {
try { return socket.getOutputStream(); }
catch (IOException e) { throw new UncheckedIOException(e); }
private OutputStream out() throws IOException {
return socket.getOutputStream();
}
private InputStream in() {
try { return socket.getInputStream(); }
catch (IOException e) { throw new UncheckedIOException(e); }
private InputStream in() throws IOException {
return socket.getInputStream();
}
private long readLen(int byteLen) {
private long readLen(int byteLen) throws IOException {
long res = 0;
try {
if (byteLen == 126) {
res |= in().read() << 8;
res |= in().read();
@ -46,23 +42,19 @@ public class WebSocket implements AutoCloseable {
}
else return byteLen;
}
catch (IOException e) { throw new UncheckedIOException(e); }
}
private byte[] readMask(boolean has) {
private byte[] readMask(boolean has) throws IOException {
if (has) {
try { return new byte[] {
return new byte[] {
(byte)in().read(),
(byte)in().read(),
(byte)in().read(),
(byte)in().read()
}; }
catch (IOException e) { throw new UncheckedIOException(e); }
};
}
else return new byte[4];
}
private void writeLength(int len) {
try {
private void writeLength(int len) throws IOException {
if (len < 126) {
out().write((int)len);
}
@ -83,10 +75,7 @@ public class WebSocket implements AutoCloseable {
out().write(len & 0xFF);
}
}
catch (IOException e) { throw new UncheckedIOException(e); }
}
private synchronized void write(int type, byte[] data) {
try {
private synchronized void write(int type, byte[] data) throws IOException {
int i;
for (i = 0; i < data.length / 0xFFFF; i++) {
@ -100,24 +89,20 @@ public class WebSocket implements AutoCloseable {
writeLength(data.length % 0xFFFF);
out().write(data, i * 0xFFFF, data.length % 0xFFFF);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void send(String data) {
public void send(String data) throws IOException {
if (closed) throw new IllegalStateException("Object is closed.");
write(1, data.getBytes());
}
public void send(byte[] data) {
public void send(byte[] data) throws IOException {
if (closed) throw new IllegalStateException("Object is closed.");
write(2, data);
}
public void send(WebSocketMessage msg) {
public void send(WebSocketMessage msg) throws IOException {
if (msg.type == Type.Binary) send(msg.binaryData());
else send(msg.textData());
}
public void send(Object data) {
public void send(Object data) throws IOException {
if (closed) throw new IllegalStateException("Object is closed.");
write(1, data.toString().getBytes());
}
@ -144,8 +129,7 @@ public class WebSocket implements AutoCloseable {
return null;
}
private byte[] readData() {
try {
private byte[] readData() throws IOException {
var maskLen = in().read();
var hasMask = (maskLen & 0x80) != 0;
var len = (int)readLen(maskLen & 0x7F);
@ -166,11 +150,8 @@ public class WebSocket implements AutoCloseable {
return null;
}
catch (IOException e) { throw new UncheckedIOException(e); }
}
public WebSocketMessage receive() {
try {
public WebSocketMessage receive() throws IOException {
var data = new ByteArrayOutputStream();
var type = 0;
@ -202,10 +183,6 @@ public class WebSocket implements AutoCloseable {
if (type == 1) return new WebSocketMessage(new String(raw));
else return new WebSocketMessage(raw);
}
}
catch (IOException e) {
close();
}
return null;
}

View File

@ -17,7 +17,6 @@ import me.topchetoeu.jscript.engine.frame.ConvertHint;
import me.topchetoeu.jscript.exceptions.ConvertException;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.exceptions.UncheckedException;
import me.topchetoeu.jscript.lib.PromiseLib;
public class Values {
@ -135,7 +134,6 @@ public class Values {
if (val instanceof String) {
try { return Double.parseDouble((String)val); }
catch (NumberFormatException e) { return Double.NaN; }
catch (Throwable e) { throw new UncheckedException(e); }
}
return Double.NaN;
}

View File

@ -11,9 +11,6 @@ public class DataNotifier<T> implements Awaitable<T> {
isErr = true;
notifier.next();
}
public void error(Throwable t) {
error(new RuntimeException(t));
}
public void next(T val) {
this.val = val;
isErr = false;

View File

@ -1,7 +0,0 @@
package me.topchetoeu.jscript.exceptions;
public class UncheckedException extends RuntimeException {
public UncheckedException(Throwable err) {
super(err);
}
}

View File

@ -1,9 +0,0 @@
package me.topchetoeu.jscript.exceptions;
import java.io.IOException;
public class UncheckedIOException extends RuntimeException {
public UncheckedIOException(IOException e) {
super(e);
}
}