7 个版本 (4 个破坏性版本)
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 日 |
#2841 在 解析器实现
72KB
2K 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();
lib.rs
:
使用文本格式。
依赖项
~5.5–7.5MB
~148K SLoC