An ES5 interpreter for Java
Go to file
2023-11-25 18:58:35 +02:00
.github/workflows fix: some annoying bugs, as well as splice 2023-11-06 00:55:30 +02:00
src fix: micro tasks not handled properly 2023-11-25 18:58:35 +02:00
tests feat: write some tests 2023-11-04 11:38:48 +02:00
.gitignore feat: include typescript code in source code 2023-11-05 20:27:23 +02:00
build.js fix: remove some unnececeary logs 2023-11-05 20:29:21 +02:00
LICENSE bruh 2023-08-26 12:19:26 +03:00
README.md refactor: some minor fixes, rewrite README example 2023-11-06 10:55:38 +02:00

JScript

NOTE: This had nothing to do with Microsoft's dialect of EcmaScript

WARNING: Currently, this code is mostly undocumented. Proceed with caution and a psychiatrist.

JScript is an engine, capable of running EcmaScript 5, written entirely in Java. This engine has been developed with the goal of being easy to integrate with your preexisting codebase, THE GOAL OF THIS ENGINE IS NOT PERFORMANCE. My crude experiments show that this engine is 50x-100x slower than V8, which, although bad, is acceptable for most simple scripting purposes. Note that although the codebase has a Main class, this isn't meant to be a standalone program, but instead a library for running JavaScript code.

Example

The following will create a REPL using the engine as a backend. Not that this won't properly log errors. I recommend checking out the implementation in Main.main:

var engine = new Engine(true /* false if you dont want debugging */);
var env = new Environment(null, null, null);
var debugger = new DebugServer();

// Create one target for the engine and start debugging server
debugger.targets.put("target", (socket, req) -> new SimpleDebugger(socket, engine));
debugger.start(new InetSocketAddress("127.0.0.1", 9229), true);

// Queue code to load internal libraries and start engine
engine.pushMsg(false, null, new Internals().getApplier(env));
engine.start();

while (true) {
    try {
        var raw = Reading.read();
        if (raw == null) break;

        // Push a message to the engine with the raw REPL code
        var res = engine.pushMsg(
            false, new Context(engine).pushEnv(env),
            new Filename("jscript", "repl.js"), raw, null
        ).await();

        Values.printValue(null, res);
    }
    catch (EngineException e) { Values.printError(e, ""); }
    catch (SyntaxException ex) {
        System.out.println("Syntax error:" + ex.msg);
    }
    catch (IOException e) { }
}