diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index 80872d7..8922290 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -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); diff --git a/src/me/topchetoeu/jscript/compilation/values/OperationStatement.java b/src/me/topchetoeu/jscript/compilation/values/OperationStatement.java index 3016ee2..97479bd 100644 --- a/src/me/topchetoeu/jscript/compilation/values/OperationStatement.java +++ b/src/me/topchetoeu/jscript/compilation/values/OperationStatement.java @@ -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); diff --git a/src/me/topchetoeu/jscript/engine/Engine.java b/src/me/topchetoeu/jscript/engine/Engine.java index c82dbde..6504b82 100644 --- a/src/me/topchetoeu/jscript/engine/Engine.java +++ b/src/me/topchetoeu/jscript/engine/Engine.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/engine/values/ObjectValue.java b/src/me/topchetoeu/jscript/engine/values/ObjectValue.java index 2f21e3f..7bdf0a3 100644 --- a/src/me/topchetoeu/jscript/engine/values/ObjectValue.java +++ b/src/me/topchetoeu/jscript/engine/values/ObjectValue.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/engine/values/Values.java b/src/me/topchetoeu/jscript/engine/values/Values.java index ec261d5..9ca731a 100644 --- a/src/me/topchetoeu/jscript/engine/values/Values.java +++ b/src/me/topchetoeu/jscript/engine/values/Values.java @@ -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(); } diff --git a/src/me/topchetoeu/jscript/events/Awaitable.java b/src/me/topchetoeu/jscript/events/Awaitable.java index 7583892..8f2d9ec 100644 --- a/src/me/topchetoeu/jscript/events/Awaitable.java +++ b/src/me/topchetoeu/jscript/events/Awaitable.java @@ -1,7 +1,9 @@ package me.topchetoeu.jscript.events; +import me.topchetoeu.jscript.exceptions.InterruptException; + public interface Awaitable { - T await() throws FinishedException, InterruptedException; + T await() throws FinishedException; default Observable toObservable() { return sub -> { @@ -10,9 +12,7 @@ public interface Awaitable { sub.next(await()); sub.finish(); } - catch (InterruptedException | FinishedException e) { - sub.finish(); - } + catch (InterruptException | FinishedException e) { sub.finish(); } catch (RuntimeException e) { sub.error(e); } diff --git a/src/me/topchetoeu/jscript/exceptions/UncheckedException.java b/src/me/topchetoeu/jscript/exceptions/UncheckedException.java new file mode 100644 index 0000000..4e730e7 --- /dev/null +++ b/src/me/topchetoeu/jscript/exceptions/UncheckedException.java @@ -0,0 +1,7 @@ +package me.topchetoeu.jscript.exceptions; + +public class UncheckedException extends RuntimeException { + public UncheckedException(Throwable err) { + super(err); + } +} diff --git a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java index 3a12212..c70043c 100644 --- a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java +++ b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java @@ -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, 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); diff --git a/src/me/topchetoeu/jscript/interop/OverloadFunction.java b/src/me/topchetoeu/jscript/interop/OverloadFunction.java index 87eebd0..4ea59e2 100644 --- a/src/me/topchetoeu/jscript/interop/OverloadFunction.java +++ b/src/me/topchetoeu/jscript/interop/OverloadFunction.java @@ -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, ""); if (e.getTargetException() instanceof EngineException) { diff --git a/src/me/topchetoeu/jscript/lib/JSONLib.java b/src/me/topchetoeu/jscript/lib/JSONLib.java index 4ce8acf..8d612df 100644 --- a/src/me/topchetoeu/jscript/lib/JSONLib.java +++ b/src/me/topchetoeu/jscript/lib/JSONLib.java @@ -74,9 +74,7 @@ public class JSONLib { try { return toJS(me.topchetoeu.jscript.json.JSON.parse("", 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) { diff --git a/src/me/topchetoeu/jscript/lib/PromiseLib.java b/src/me/topchetoeu/jscript/lib/PromiseLib.java index 40afcd4..1b53d2a 100644 --- a/src/me/topchetoeu/jscript/lib/PromiseLib.java +++ b/src/me/topchetoeu/jscript/lib/PromiseLib.java @@ -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 { diff --git a/src/me/topchetoeu/jscript/lib/RegExpLib.java b/src/me/topchetoeu/jscript/lib/RegExpLib.java index 39284f1..9828577 100644 --- a/src/me/topchetoeu/jscript/lib/RegExpLib.java +++ b/src/me/topchetoeu/jscript/lib/RegExpLib.java @@ -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;