#wasm-bindings #codec #wasm-binary #ast #web-idl #binary-format #decoder

wasm-webidl-bindings

带有AST的原始WebIDL绑定二进制编码器/解码器。仍然处于变化状态!

11个版本 (7个破坏性版本)

0.8.0 2020年2月3日
0.7.0 2020年2月3日
0.6.0 2019年11月4日
0.5.0 2019年9月11日
0.1.2 2019年7月15日

#1538 in WebAssembly

每月46次下载
wasmtime-interface-types 中使用

MIT/Apache

205KB
5K SLoC

wasm-webidl-bindings

读取、写入和操作Wasm WebIDL绑定自定义部分。

内容概览

  • 用于解析straw提案文本格式的解析器。请参阅 crates/text-parser/src/grammar.lalrpop

  • 用于表示和操作WebIDL绑定的AST类型集。请参阅 src/ast.rs

  • 用于straw提案二进制格式的编码器和解码器。请参阅 src/binary/encode.rs 中的实现以及 BINARY.md 中的格式详情。

示例

解析文本格式并将其编码为二进制格式

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

依赖项

~3.5–5MB
~94K SLoC