When using jsii-pacmak to generate libraries in different programming
languages, the Javascript code is bundled within the generated library, so
that it can be used during at runtime. This is the reason why a node runtime
needs to be available in order to execute code that depends on jsii libraries.
The generated libraries have a dependency on a Runtime client library for the
language, which contains the necessary logic to start a child node process
with the jsii-runtime. The jsii-runtime manages JSON-based inter-process
communication over its STDIN and STDOUT, and manages a @jsii/kernel
instance that acts as a container for the Javascript code that backs the
jsii libraries.
A simplified representation of the execution environment of an application using jsii libraries from a different language follows:
┌────────────────────────┐ ┌────────────┬────┬────┬───┐
│ │ │ │ │ │ │
│ User's Application │ │@jsii/kernel│LibA│LibB│...│
│ │ │ │ │ │ │
│ ┌──────────────────┤ ├────────────┴────┴────┴───┤
│ │ │ │ │
│ │Generated Bindings│ │ @jsii/runtime │
│ │ │ │ │
│ ├──────────────────┤ ├────────┬─────────────────┤
│ │ ├────────────▶│ STDIN │ │
│ │Host jsii Runtime │ JSON ├────────┤ │
│ │ │◀────────────┤ STDOUT │ node │
├─────┴──────────────────┤ ├────────┘ │
│ │ │ (Child Process) │
│ JVM / .NET / ... │ │ │
│ │ │ │
├────────────────────────┴─────────────┴──────────────────────────┤
│ │
│ Operating System │
│ │
└─────────────────────────────────────────────────────────────────┘
The initialization workflow can be described as:
- The host (Java, .NET, ...) application starts on its own runtime (JVM, .NET Runtime, ...)
- When the host code encounters a jsii entity for the first time (creating
an instance of a jsii type, loading a static constant, ...), the runtime
client library creates a child
nodeprocess, and loads thejsii-runtimelibrary (specified by theJSII_RUNTIMEenvironment variable, or the version that is bundled in the runtime client library) - The runtime client library interacts with the child
nodeprocess by exchanging JSON-encoded messages through thenodeprocess' STDIN and STDOUT - The runtime client library automatically loads the Javascript modules
bundled within the generated bindings (and their depedencies, bundled in
other generated bindings) into the
nodeprocess when needed. - Calls into the Generated bindings are encoded into JSON requests and sent
to the child
nodeprocess, which will execute the corresponding Javascript code, then responds back. - Upon exiting, the host process closes the communication channels with the
child
nodeprocess, causing it to exit.