A simple WebAssembly wrapper for the Rust wit-bindgen package, designed to provide basic WIT (WebAssembly Interface Type) validation and parsing functionality in web environments.
- Basic WIT syntax validation
- Interface name extraction
- World definition detection
- WebAssembly-optimized build
To build the WebAssembly module:
# Install build tools (specific versions for consistency)
npm run setup-wasm
# Build the WebAssembly component
npm run build-wasmThe library provides a simple API through the WitBindgen class:
import init, { WitBindgen } from './pkg/wit_bindgen_wasm.js';
async function main() {
// Initialize the WASM module
await init();
// Create a new instance
const witBindgen = new WitBindgen();
// Validate WIT syntax
const isValid = witBindgen.validate_wit_syntax(`
interface calculator {
add: func(a: u32, b: u32) -> u32;
}
`);
console.log('Is valid WIT:', isValid);
// Extract interface names
const interfaces = witBindgen.extract_interfaces(witContent);
console.log('Interfaces found:', interfaces);
}
main();new()- Create a new instancevalidate_wit_syntax(content: string): boolean- Basic WIT syntax validationextract_interfaces(content: string): string- Extract comma-separated interface nameshas_world_definition(content: string): boolean- Check for world definitionsversion(): string- Get package versiongenerate_bindings(content: string, language: string, world_name?: string): string- Generate language bindings (returns JSON with file map)- Supported languages:
rust,c,cpp(orc++),csharp(orc#),go,moonbit,markdown(ormd)
- Supported languages:
IMPORTANT: The generate_bindings function returns file content that is encoded using Latin1 (ISO-8859-1) encoding to ensure binary data integrity. This allows both text and binary files (like .wasm, .o) to be safely transmitted through the WebAssembly string interface without corruption.
When consuming the output:
const result = JSON.parse(witBindgen.generate_bindings(content, "rust"));
for (const [filename, content] of Object.entries(result.files)) {
// Content is Latin1-encoded - decode properly when writing to disk
const buffer = Buffer.from(content, 'latin1');
fs.writeFileSync(filename, buffer);
}is_wit_file_extension(filename: string): boolean- Check if filename has .wit extensionget_wit_bindgen_version(): string- Get library version
This is a minimal wrapper designed to be extended in future sessions. The current implementation provides basic functionality for WIT content analysis without full parsing capabilities.
Apache-2.0 WITH LLVM-exception