Skip to content
Read, write, and manipulate the Wasm WebIDL bindings custom section
Branch: master
Clone or download
Latest commit 2406ce1 Aug 13, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
ci Add azure pipelines CI config May 13, 2019
crates/text-parser Add Cargo.toml metadata Jul 15, 2019
src Update walrus to 0.11.0 Aug 13, 2019
.gitignore Initial commit; mostly stubbed out and missing May 13, 2019 Use "scalar" instead of "primitive" in the binary format docs May 21, 2019
Cargo.toml Bump to 0.4.0 Aug 13, 2019
LICENSE-APACHE Add LICENSE files May 13, 2019
LICENSE-MIT Add LICENSE files May 13, 2019 Update "What's Inside" to include the binary decoder Jul 3, 2019
README.tpl Initial commit; mostly stubbed out and missing May 13, 2019
azure-pipelines.yml Make sure to test with all features enabled in CI Jun 24, 2019


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

Build Status

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/

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


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
                (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
                (as any 0)
                (as any 1)
                (view Int8Array 2 3))
                (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.


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();
You can’t perform that action at this time.