Skip to main content

ling_codegen/
wasm.rs

1//! Ling → WebAssembly (binary format) emitter.
2
3use crate::{CodegenBackend, MirProgram};
4
5/// Minimal WASM binary writer.
6pub struct WasmBackend {
7    buf: Vec<u8>,
8}
9
10impl WasmBackend {
11    pub fn new() -> Self {
12        let buf = vec![
13            0x00, 0x61, 0x73, 0x6D, // magic: \0asm
14            0x01, 0x00, 0x00, 0x00, // version: 1
15        ];
16        Self { buf }
17    }
18}
19
20impl Default for WasmBackend {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl CodegenBackend for WasmBackend {
27    fn emit(&mut self, _mir: &MirProgram, out: &std::path::Path) -> anyhow::Result<()> {
28        std::fs::write(out.with_extension("wasm"), &self.buf)?;
29        Ok(())
30    }
31}