refactor: clean up Main class

This commit is contained in:
TopchetoEU 2023-11-13 18:56:59 +02:00
parent df9932874d
commit 1acd78e119
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4

View File

@ -18,13 +18,8 @@ import me.topchetoeu.jscript.exceptions.InterruptException;
import me.topchetoeu.jscript.exceptions.SyntaxException; import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.lib.Internals; import me.topchetoeu.jscript.lib.Internals;
public class Main { public class Main {
static Thread engineTask, debugTask; public static class Printer implements Observer<Object> {
static Engine engine;
static Environment env;
static int j = 0;
private static Observer<Object> valuePrinter = new Observer<Object>() {
public void next(Object data) { public void next(Object data) {
Values.printValue(null, data); Values.printValue(null, data);
System.out.println(); System.out.println();
@ -34,27 +29,80 @@ public class Main {
Values.printError(err, null); Values.printError(err, null);
} }
@Override
public void finish() { public void finish() {
engineTask.interrupt(); engineTask.interrupt();
} }
}; }
public static void main(String args[]) { static Thread engineTask, debugTask;
System.out.println(String.format("Running %s v%s by %s", Metadata.NAME, Metadata.VERSION, Metadata.AUTHOR)); static Engine engine = new Engine(true);
engine = new Engine(true); static DebugServer debugServer = new DebugServer();
static Environment environment = new Environment(null, null, null);
var exited = new boolean[1]; static int j = 0;
var server = new DebugServer(); static boolean exited = false;
server.targets.put("target", (ws, req) -> new SimpleDebugger(ws, engine)); static String[] args;
env = Internals.apply(new Environment(null, null, null));
env.global.define("exit", _ctx -> { private static void reader() {
exited[0] = true; try {
for (var arg : args) {
try {
if (arg.equals("--ts")) initTypescript();
else {
var file = Path.of(arg);
var raw = Files.readString(file);
var res = engine.pushMsg(
false, new Context(engine, environment),
Filename.fromFile(file.toFile()),
raw, null
).await();
Values.printValue(null, res);
System.out.println();
}
}
catch (EngineException e) { Values.printError(e, ""); }
}
for (var i = 0; ; i++) {
try {
var raw = Reading.read();
if (raw == null) break;
var res = engine.pushMsg(
false, new Context(engine, environment),
new Filename("jscript", "repl/" + i + ".js"),
raw, null
).await();
Values.printValue(null, res);
System.out.println();
}
catch (EngineException e) { Values.printError(e, ""); }
catch (SyntaxException e) { Values.printError(e, ""); }
}
}
catch (IOException e) {
System.out.println(e.toString());
exited = true;
}
catch (RuntimeException ex) {
if (!exited) {
System.out.println("Internal error ocurred:");
ex.printStackTrace();
}
}
if (exited) {
debugTask.interrupt();
engineTask.interrupt();
}
}
private static void initEnv() {
environment = Internals.apply(environment);
environment.global.define("exit", _ctx -> {
exited = true;
throw new InterruptException(); throw new InterruptException();
}); });
env.global.define("go", _ctx -> { environment.global.define("go", _ctx -> {
try { try {
var f = Path.of("do.js"); var f = Path.of("do.js");
var func = _ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f))); var func = _ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f)));
@ -64,10 +112,13 @@ public class Main {
throw new EngineException("Couldn't open do.js"); throw new EngineException("Couldn't open do.js");
} }
}); });
}
private static void initEngine() {
debugServer.targets.put("target", (ws, req) -> new SimpleDebugger(ws, engine));
engineTask = engine.start(); engineTask = engine.start();
debugTask = server.start(new InetSocketAddress("127.0.0.1", 9229), true); debugTask = debugServer.start(new InetSocketAddress("127.0.0.1", 9229), true);
}
private static void initTypescript() {
try { try {
var tsEnv = Internals.apply(new Environment(null, null, null)); var tsEnv = Internals.apply(new Environment(null, null, null));
var bsEnv = Internals.apply(new Environment(null, null, null)); var bsEnv = Internals.apply(new Environment(null, null, null));
@ -84,48 +135,27 @@ public class Main {
engine.pushMsg( engine.pushMsg(
false, ctx, false, ctx,
new Filename("jscript", "internals/bootstrap.js"), Reading.resourceToString("js/bootstrap.js"), null, new Filename("jscript", "internals/bootstrap.js"), Reading.resourceToString("js/bootstrap.js"), null,
tsEnv.global.get(ctx, "ts"), env, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts")) tsEnv.global.get(ctx, "ts"), environment, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts"))
).await(); ).await();
} }
catch (EngineException e) { catch (EngineException e) {
Values.printError(e, "(while initializing TS)"); Values.printError(e, "(while initializing TS)");
} }
}
var reader = new Thread(() -> { private static void initReader() {
try { var reader = new Thread(Main::reader);
for (var arg : args) {
try {
var file = Path.of(arg);
var raw = Files.readString(file);
valuePrinter.next(engine.pushMsg(false, new Context(engine).pushEnv(env), Filename.fromFile(file.toFile()), raw, null).await());
}
catch (EngineException e) { Values.printError(e, ""); }
}
for (var i = 0; ; i++) {
try {
var raw = Reading.read();
if (raw == null) break;
valuePrinter.next(engine.pushMsg(false, new Context(engine).pushEnv(env), new Filename("jscript", "repl/" + i + ".js"), raw, null).await());
}
catch (EngineException e) { Values.printError(e, ""); }
}
}
catch (IOException e) { exited[0] = true; }
catch (SyntaxException ex) {
if (exited[0]) return;
System.out.println("Syntax error:" + ex.msg);
}
catch (RuntimeException ex) {
if (!exited[0]) {
System.out.println("Internal error ocurred:");
ex.printStackTrace();
}
}
if (exited[0]) debugTask.interrupt();
});
reader.setDaemon(true); reader.setDaemon(true);
reader.setName("STD Reader"); reader.setName("STD Reader");
reader.start(); reader.start();
} }
public static void main(String args[]) {
System.out.println(String.format("Running %s v%s by %s", Metadata.NAME, Metadata.VERSION, Metadata.AUTHOR));
Main.args = args;
initEnv();
initEngine();
initReader();
}
} }