Expand description
WebAssembly Code Generation for JavaScript
This module provides functionality to emit Wasm modules which will run JavaScript source code with the QuickJS interpreter.
Javy supports two main code generation paths:
- Static code generation
- Dynamic code generation
§Static code generation
A single unit of code is generated, which is a Wasm module consisting of the bytecode representation of a given JavaScript program and the code for a particular version of the QuickJS engine compiled to Wasm.
The generated Wasm module is self contained and the bytecode version matches the exact requirements of the embedded QuickJs engine.
§Dynamic code generation
A single unit of code is generated, which is a Wasm module consisting of the bytecode representation of a given JavaScript program. The JavaScript bytecode is stored as part of the data section of the module which also contains instructions to execute that bytecode through dynamic linking at runtime.
Dynamic code generation requires a plugin module to be used and linked against at runtime in order to execute the JavaScript bytecode. This operation involves carefully ensuring that a given plugin version matches the plugin version of the imports requested by the generated Wasm module as well as ensuring that any features available in the plugin match the features requsted by the JavaScript bytecode.
§Examples
Simple Wasm module generation:
use std::path::Path;
use javy_codegen::{Generator, LinkingKind, Plugin, JS};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load your target Javascript.
let js = JS::from_file(Path::new("example.js"))?;
// Load existing pre-initialized Javy plugin.
let plugin = Plugin::new_from_path(Path::new("example-plugin.wasm"))?;
// Configure code generator.
let mut generator = Generator::new(plugin);
generator.linking(LinkingKind::Static);
// Generate your Wasm module.
let wasm = generator.generate(&js);
Ok(())
}
§Core concepts
Generator
- The main entry point for generating Wasm modules.Plugin
- An initialized Javy plugin.JS
- JavaScript source code.
§Features
plugin_internal
- Enables additional code generation options for internal use. Please note that this flag enables an unstable feature. The unstable API’s exposed by this future may break in the future without notice.
Structs§
- Generator
- Generator used to produce Wasm binaries from JS source code.
- JS
- JS source code.
- Plugin
- A Javy plugin.
- WitOptions
- Options for using WIT in the code generation process.
Enums§
- Linking
Kind - The kind of linking to use.