w2c2-rs
w2c2-rs is a Rust wrapper for w2c2, a WebAssembly to C compiler. This project provides low-level bindings (w2c2-sys) to the underlying C library, enabling Rust applications to compile WebAssembly modules into C code.
Workspace Crates
This repository is a Cargo workspace containing the following crates:
w2c2: A high-level, safe Rust wrapper forw2c2(currently under development).w2c2-sys: Low-level, unsafe bindings to thew2c2C library, generated usingbindgen.example-simple: A basic example demonstrating how to use thew2c2-syscrate to compile a WebAssembly module.
Features
The w2c2-sys crate provides the following Cargo features:
bindgen: Enables the automatic regeneration of Rust bindings from the C source code. This is useful for developers who want to update the underlying C library or customize the bindings.
Usage
Here is a basic example of how to use w2c2-sys to compile a WebAssembly module to C code. This example is based on the example-simple crate.
1. Add w2c2-sys to your Cargo.toml
[]
= { = "0.1.0", = "path/to/w2c2-sys" }
2. Compile a WebAssembly Module
The following Rust code demonstrates how to load a WebAssembly module, parse it, and compile it into a C source file named test.c.
use ;
use ConstDefault;
use ;
const WASM: & = &const_wat2wasm!;
Workflow
The following diagram illustrates the compilation workflow:
graph TD
A[Start] --> B{Load .wasm bytes};
B --> C{Create Buffer};
C --> D{Instantiate WasmModuleReader};
D --> E{Call wasmModuleRead};
E --> F{Check for errors};
F -- No errors --> G{Call wasmCWriteModule};
G --> H[Output test.c];
F -- Error --> I[Print error message];
H --> J[End];
I --> J;
Building from Source
The w2c2 C library is automatically compiled and linked by the build.rs script in the w2c2-sys crate. No manual compilation of the C source code is required.
License
This project is licensed under the MIT License.