Debugging support #7

Merged
TopchetoEU merged 7 commits from TopcehtoEU/debugging into master 2023-10-28 14:11:55 +00:00
12 changed files with 34 additions and 56 deletions
Showing only changes of commit a4e5f7f471 - Show all commits

View File

@ -16,6 +16,7 @@ import me.topchetoeu.jscript.events.Observer;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.InterruptException;
import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.exceptions.UncheckedException;
import me.topchetoeu.jscript.lib.Internals;
public class Main {
@ -35,9 +36,7 @@ public class Main {
br.close();
return out.toString();
}
catch (IOException e) {
return null;
}
catch (Throwable e) { throw new UncheckedException(e); }
}
public static String resourceToString(String name) {
var str = Main.class.getResourceAsStream("/me/topchetoeu/jscript/" + name);
@ -97,20 +96,17 @@ public class Main {
catch (EngineException e) { Values.printError(e, ""); }
}
}
catch (IOException e) {
e.printStackTrace();
return;
}
catch (InterruptException e) { return; }
catch (SyntaxException ex) {
if (exited[0]) return;
System.out.println("Syntax error:" + ex.msg);
}
catch (InterruptException e) { return; }
catch (RuntimeException ex) {
if (exited[0]) return;
System.out.println("Internal error ocurred:");
ex.printStackTrace();
}
catch (Throwable e) { throw new UncheckedException(e); }
if (exited[0]) return;
});
reader.setDaemon(true);

View File

@ -49,13 +49,8 @@ public class OperationStatement extends Statement {
vals[i] = ((ConstantStatement)args[i]).value;
}
try {
return new ConstantStatement(loc(), Values.operation(null, operation, vals));
}
catch (EngineException e) {
return new ThrowStatement(loc(), new ConstantStatement(loc(), e.value));
}
catch (InterruptedException e) { return null; }
try { return new ConstantStatement(loc(), Values.operation(null, operation, vals)); }
catch (EngineException e) { return new ThrowStatement(loc(), new ConstantStatement(loc(), e.value)); }
}
return new OperationStatement(loc(), operation, args);

View File

@ -8,6 +8,7 @@ import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.events.Awaitable;
import me.topchetoeu.jscript.events.DataNotifier;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.InterruptException;
public class Engine {
private class UncompiledFunction extends FunctionValue {
@ -76,7 +77,7 @@ public class Engine {
}
catch (InterruptedException e) {
for (var msg : macroTasks) {
msg.notifier.error(new RuntimeException(e));
msg.notifier.error(new InterruptException(e));
}
break;
}

View File

@ -155,9 +155,7 @@ public class ObjectValue {
if (prototype == SYNTAX_ERR_PROTO) return ctx.environment().proto("syntaxErr");
if (prototype == TYPE_ERR_PROTO) return ctx.environment().proto("typeErr");
}
catch (NullPointerException e) {
return null;
}
catch (NullPointerException e) { return null; }
return (ObjectValue)prototype;
}

View File

@ -15,8 +15,8 @@ import me.topchetoeu.jscript.engine.Operation;
import me.topchetoeu.jscript.engine.frame.ConvertHint;
import me.topchetoeu.jscript.exceptions.ConvertException;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.InterruptException;
import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.exceptions.UncheckedException;
public class Values {
public static final Object NULL = new Object();
@ -97,12 +97,8 @@ public class Values {
var second = hint == ConvertHint.VALUEOF ? "toString" : "valueOf";
if (ctx != null) {
try {
return tryCallConvertFunc(ctx, obj, first);
}
catch (EngineException unused) {
return tryCallConvertFunc(ctx, obj, second);
}
try { return tryCallConvertFunc(ctx, obj, first); }
catch (EngineException unused) { return tryCallConvertFunc(ctx, obj, second); }
}
throw EngineException.ofType("Value couldn't be converted to a primitive.");
@ -120,10 +116,8 @@ public class Values {
if (val instanceof Number) return number(val);
if (val instanceof Boolean) return ((Boolean)val) ? 1 : 0;
if (val instanceof String) {
try {
return Double.parseDouble((String)val);
}
catch (NumberFormatException e) { }
try { return Double.parseDouble((String)val); }
catch (Throwable e) { throw new UncheckedException(e); }
}
return Double.NaN;
}
@ -565,10 +559,6 @@ public class Values {
}
};
}
catch (InterruptException e) {
Thread.currentThread().interrupt();
return null;
}
catch (IllegalArgumentException | NullPointerException e) {
return Collections.emptyIterator();
}

View File

@ -1,7 +1,9 @@
package me.topchetoeu.jscript.events;
import me.topchetoeu.jscript.exceptions.InterruptException;
public interface Awaitable<T> {
T await() throws FinishedException, InterruptedException;
T await() throws FinishedException;
default Observable<T> toObservable() {
return sub -> {
@ -10,9 +12,7 @@ public interface Awaitable<T> {
sub.next(await());
sub.finish();
}
catch (InterruptedException | FinishedException e) {
sub.finish();
}
catch (InterruptException | FinishedException e) { sub.finish(); }
catch (RuntimeException e) {
sub.error(e);
}

View File

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

View File

@ -9,6 +9,7 @@ import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.NativeFunction;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.UncheckedException;
public class NativeWrapperProvider implements WrappersProvider {
private final HashMap<Class<?>, FunctionValue> constructors = new HashMap<>();
@ -120,7 +121,7 @@ public class NativeWrapperProvider implements WrappersProvider {
var init = overload.getAnnotation(NativeInit.class);
if (init == null || init.value() != InitType.PROTOTYPE) continue;
try { overload.invoke(null, ctx, res); }
catch (ReflectiveOperationException e) { e.printStackTrace(); }
catch (Throwable e) { throw new UncheckedException(e); }
}
applyMethods(ctx, true, res, clazz);
@ -152,7 +153,7 @@ public class NativeWrapperProvider implements WrappersProvider {
var init = overload.getAnnotation(NativeInit.class);
if (init == null || init.value() != InitType.CONSTRUCTOR) continue;
try { overload.invoke(null, ctx, func); }
catch (ReflectiveOperationException e) { e.printStackTrace(); }
catch (Throwable e) { throw new UncheckedException(e); }
}
if (((OverloadFunction)func).overloads.size() == 0) {
@ -180,7 +181,7 @@ public class NativeWrapperProvider implements WrappersProvider {
var init = overload.getAnnotation(NativeInit.class);
if (init == null || init.value() != InitType.NAMESPACE) continue;
try { overload.invoke(null, ctx, res); }
catch (ReflectiveOperationException e) { e.printStackTrace(); }
catch (Throwable e) { throw new UncheckedException(e); }
}
applyMethods(ctx, false, res, clazz);

View File

@ -76,12 +76,8 @@ public class OverloadFunction extends FunctionValue {
try {
return Values.normalize(ctx, overload.runner.run(ctx, _this, newArgs));
}
catch (InstantiationException e) {
throw EngineException.ofError("The class may not be instantiated.");
}
catch (IllegalAccessException | IllegalArgumentException e) {
continue;
}
catch (InstantiationException e) { throw EngineException.ofError("The class may not be instantiated."); }
catch (IllegalAccessException | IllegalArgumentException e) { continue; }
catch (InvocationTargetException e) {
var loc = new Location(0, 0, "<internal>");
if (e.getTargetException() instanceof EngineException) {

View File

@ -74,9 +74,7 @@ public class JSONLib {
try {
return toJS(me.topchetoeu.jscript.json.JSON.parse("<value>", val));
}
catch (SyntaxException e) {
throw EngineException.ofSyntax(e.msg);
}
catch (SyntaxException e) { throw EngineException.ofSyntax(e.msg); }
}
@Native
public static String stringify(Context ctx, Object val) {

View File

@ -186,9 +186,7 @@ public class PromiseLib {
if (thisArg instanceof PromiseLib) ((PromiseLib)thisArg).handle(ctx, fulfillHandle, rejectHandle);
else {
Object next;
try {
next = Values.getMember(ctx, thisArg, "then");
}
try { next = Values.getMember(ctx, thisArg, "then"); }
catch (IllegalArgumentException e) { next = null; }
try {

View File

@ -97,9 +97,7 @@ public class RegExpLib {
var groups = new ObjectValue();
for (var el : namedGroups) {
try {
groups.defineProperty(null, el, matcher.group(el));
}
try { groups.defineProperty(null, el, matcher.group(el)); }
catch (IllegalArgumentException e) { }
}
if (groups.values.size() == 0) groups = null;