#binding #bindings #ast #binary #state #raw #read #webidl #flux #read-write

wasm-webidl-bindings

Raw WebIDL bindings binary encoder/decoder with an AST. Still in a state of flux!

9 releases (5 breaking)

✓ Uses Rust 2018 edition

0.6.0 Nov 4, 2019
0.5.0 Sep 11, 2019
0.4.0 Aug 13, 2019
0.3.1 Jul 30, 2019
0.1.2 Jul 15, 2019

#24 in WebAssembly

Download history 247/week @ 2019-07-22 563/week @ 2019-07-29 542/week @ 2019-08-05 615/week @ 2019-08-12 1029/week @ 2019-08-19 1082/week @ 2019-08-26 887/week @ 2019-09-02 1065/week @ 2019-09-09 1272/week @ 2019-09-16 2364/week @ 2019-09-23 1186/week @ 2019-09-30 1116/week @ 2019-10-07 934/week @ 2019-10-14 1373/week @ 2019-10-21 1404/week @ 2019-10-28

4,936 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

205KB
5K SLoC

wasm-webidl-bindings

Read, write, and manipulate the Wasm WebIDL bindings custom section.

What's Inside

  • A parser for the straw proposal text format. See crates/text-parser/src/grammar.lalrpop.

  • A set of AST types for representing and manipulating WebIDL bindings. See src/ast.rs.

  • An encoder and decoder for the straw proposal binary format. See the implementation at src/binary/encode.rs and details on the format at BINARY.md.

Example

Parsing the Text Format and Encoding it in the Binary Format

#[cfg(feature = "text")]
use wasm_webidl_bindings::{binary, text};

// Get the `walrus::Module` that this webidl-bindings section is for.
//
// The Wasm type and func that are being bound are:
//
//     (type $EncodeIntoFuncWasm
//       (param anyref anyref i32 i32)
//       (result i64 i64))
//
//     (func $encodeInto
//       (import "TextEncoder" "encodeInto")
//       (type $EncodeIntoFuncWasm))
let raw_wasm: Vec<u8> = get_wasm_buffer_from_somewhere();

let mut config = walrus::ModuleConfig::default();

// Register a function to run after the module is parsed, but with access to the
// mapping from indices in the original Wasm binary to their newly assigned
// walrus IDs.
//
// This is where we will parse the Web IDL bindings text.
config.on_parse(|module, indices_to_ids| {
    let webidl_bindings = text::parse(module, indices_to_ids, r#"
        type $TextEncoderEncodeIntoResult
            (dict
                (field "read" unsigned long long)
                (field "written" unsigned long long))

        type $EncodeIntoFuncWebIDL
            (func (method any)
                (param USVString Uint8Array)
                (result $TextEncoderEncodeIntoResult))

        func-binding $encodeIntoBinding import $EncodeIntoFuncWasm $EncodeIntoFuncWebIDL
            (param
                (as any 0)
                (as any 1)
                (view Int8Array 2 3))
            (result
                (as i64 (field 0 (get 0)))
                (as i64 (field 1 (get 0))))

        bind $encodeInto $encodeIntoBinding
    "#)?;

    println!("The parsed Web IDL bindings are {:#?}", webidl_bindings);

    // Insert the `webidl_bindings` into the module as a custom section.
    module.customs.add(webidl_bindings);

    Ok(())
});

let mut module = config.parse(&raw_wasm)?;

// Reserialize the Wasm module along with its new Web IDL bindings
// section.
let new_raw_wasm = module.emit_wasm();

Dependencies

~2–2.7MB
~57K SLoC