Unit content
Embedding language runtimes and native extensions
Compiled and interpreted code can coexist inside one process when a language runtime exposes an interface that native code can call.
Embedding an interpreter
A compiled host program can initialize an interpreter or virtual machine, provide source or bytecode, exchange values with it and ask it to execute functions.
native host
↕ runtime API
interpreter / VM
↕
scripted code
Native extensions
The direction can also be reversed: code running inside a language runtime can call a native extension compiled to machine code.
Crossing the boundary
Values often need conversion between runtime-managed objects and native representations. Ownership, garbage collection, exceptions and thread rules must remain valid on both sides.
Embedding is therefore different from merely launching another program. The two languages share one process and cooperate through an in-process runtime API.
This pattern lets applications combine a compiled systems core with scripting, plugins or high-level orchestration without requiring the whole application to use one language.