Git Product home page Git Product logo

wasm-webidl-bindings's Introduction

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();

wasm-webidl-bindings's People

Contributors

alexcrichton avatar fitzgen avatar freebroccolo avatar hywan avatar joshtriplett avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.