I've been looking at the GraalVM reference for JS and most of the examples show creating a context and executing js code within that context.
Context context = Context.create()
Is there a way of directly compiling the JS code to something like a bytecode equivalent, loading it and running it directly?
GraalVM languages, JavaScript, Ruby, R, Python, Webassembly, GraalSqueak, and so on don't compile the target program into the bytecode.
The language implementation is an interpreter for the abstract syntax tree for the target language. This interpreter is written in Java using the Truffle framework API.
At runtime you construct the program AST using the nodes of this interpreter and they know how to evaluate themselves. For example something like a + b
could become 3 node objects, AddNode
and 2 children node. And AddNode.evaluate()
could be something like return left.evaluate() + right evaluate()
.
You can interpret the tree of the target program and that's how it is executed. At runtime the interpreter can modify the tree, optimizing the execution after gathering some profile for what's going to be executed. It sort of fuzes together the interpreter code and the data from the program using a technique called partial evaluation (which you can think of as a very comprehensive inlining).
Then the interpreter code is further JIT-compiled with the JVM jit compiler. But there's no intermediate bytecode representation for the program.
Of course you can use the js
utility from GraalVM to run the JavaScript code directly with it. Or node.js
from GraalVM (or otherwise). But I think this is not what is asked here.