Java / GraalVM Interview questions
Explain the execution flow of a polyglot call between Java and JavaScript on GraalVM?
A polyglot call walks through several layers before returning a result back to Java host code.
sequenceDiagram
participant Host as Java Host Code
participant Ctx as polyglot.Context
participant JS as GraalJS (Truffle)
participant Graal as Graal Compiler
Host->>Ctx: context.eval("js", source)
Ctx->>JS: parse source into Truffle AST
JS->>JS: interpret AST nodes, specialize on types
JS->>Graal: hot nodes handed off for partial evaluation
Graal-->>JS: optimized machine code installed
JS-->>Ctx: result value produced
Ctx-->>Host: wrapped as org.graalvm.polyglot.Value- Java host code calls
context.eval("js", source), handing the JavaScript source string to the Context. - GraalJS, a Truffle interpreter, parses it into an AST and begins tree-walking execution, with nodes specializing themselves based on observed operand types.
- Once a code path is hot, the Graal compiler partially evaluates the interpreter against that specific AST, producing optimized machine code.
- The final result is wrapped in a
Valueobject and handed back to the Java host, which can convert it to a native Java type or a proxying interface.
Calls in the opposite direction (JavaScript calling into Java objects) work symmetrically, going through the same Context's host-access policy.
More Related questions...