mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-20 16:26:33 +00:00
Implement support for WebIDL dictionaries
This commit adds support for generating bindings for dictionaries defined in WebIDL. Dictionaries are associative arrays which are simply objects in JS with named keys and some values. In Rust given a dictionary like: dictionary Foo { long field; }; we'll generate a struct like: pub struct Foo { obj: js_sys::Object, } impl Foo { pub fn new() -> Foo { /* make a blank object */ } pub fn field(&mut self, val: i32) -> &mut Self { // set the field using `js_sys::Reflect` } } // plus a bunch of AsRef, From, and wasm abi impls At the same time this adds support for partial dictionaries and dictionary inheritance. All dictionary fields are optional by default and hence only have builder-style setters, but dictionaries can also have required fields. Required fields are exposed as arguments to the `new` constructor. Closes #241
This commit is contained in:
@ -21,6 +21,10 @@ pub struct Program {
|
||||
pub consts: Vec<Const>,
|
||||
/// rust submodules
|
||||
pub modules: Vec<Module>,
|
||||
/// "dictionaries", generated for WebIDL, which are basically just "typed
|
||||
/// objects" in the sense that they represent a JS object with a particular
|
||||
/// shape in JIT parlance.
|
||||
pub dictionaries: Vec<Dictionary>,
|
||||
}
|
||||
|
||||
/// A rust to js interface. Allows interaction with rust objects/functions
|
||||
@ -253,6 +257,21 @@ pub struct Module {
|
||||
pub imports: Vec<Import>,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
#[derive(Clone)]
|
||||
pub struct Dictionary {
|
||||
pub name: Ident,
|
||||
pub fields: Vec<DictionaryField>,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
|
||||
#[derive(Clone)]
|
||||
pub struct DictionaryField {
|
||||
pub name: Ident,
|
||||
pub required: bool,
|
||||
pub ty: syn::Type,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
pub(crate) fn shared(&self) -> Result<shared::Program, Diagnostic> {
|
||||
Ok(shared::Program {
|
||||
|
Reference in New Issue
Block a user