mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-24 10:11:34 +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:
@ -10,6 +10,7 @@ path = 'lib.rs'
|
||||
|
||||
[build-dependencies]
|
||||
wasm-bindgen-webidl = { path = '../webidl' }
|
||||
env_logger = "0.5"
|
||||
|
||||
[dev-dependencies]
|
||||
js-sys = { path = '../js-sys' }
|
||||
@ -19,4 +20,3 @@ wasm-bindgen-test = { path = '../test' }
|
||||
[[test]]
|
||||
name = 'wasm'
|
||||
path = 'main.rs'
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
extern crate wasm_bindgen_webidl;
|
||||
extern crate env_logger;
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
@ -6,6 +7,7 @@ use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
let idls = fs::read_dir(".")
|
||||
.unwrap()
|
||||
.map(|f| f.unwrap().path())
|
||||
|
22
crates/webidl-tests/dictionary.js
Normal file
22
crates/webidl-tests/dictionary.js
Normal file
@ -0,0 +1,22 @@
|
||||
const assert = require('assert');
|
||||
|
||||
global.assert_dict_c = function(c) {
|
||||
assert.strictEqual(c.a, 1);
|
||||
assert.strictEqual(c.b, 2);
|
||||
assert.strictEqual(c.c, 3);
|
||||
assert.strictEqual(c.d, 4);
|
||||
assert.strictEqual(c.e, 5);
|
||||
assert.strictEqual(c.f, 6);
|
||||
assert.strictEqual(c.g, 7);
|
||||
assert.strictEqual(c.h, 8);
|
||||
};
|
||||
|
||||
global.mk_dict_a = function() {
|
||||
return {};
|
||||
};
|
||||
|
||||
global.assert_dict_required = function(c) {
|
||||
assert.strictEqual(c.a, 3);
|
||||
assert.strictEqual(c.b, "a");
|
||||
assert.strictEqual(c.c, 4);
|
||||
};
|
54
crates/webidl-tests/dictionary.rs
Normal file
54
crates/webidl-tests/dictionary.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use wasm_bindgen_test::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/dictionary.rs"));
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
fn assert_dict_c(c: &C);
|
||||
#[wasm_bindgen(js_name = assert_dict_c)]
|
||||
fn assert_dict_c2(c: C);
|
||||
#[wasm_bindgen(js_name = assert_dict_c)]
|
||||
fn assert_dict_c3(c: Option<&C>);
|
||||
#[wasm_bindgen(js_name = assert_dict_c)]
|
||||
fn assert_dict_c4(c: Option<C>);
|
||||
fn mk_dict_a() -> A;
|
||||
#[wasm_bindgen(js_name = mk_dict_a)]
|
||||
fn mk_dict_a2() -> Option<A>;
|
||||
fn assert_dict_required(r: &Required);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn smoke() {
|
||||
A::new().c(1).g(2).h(3).d(4);
|
||||
B::new().c(1).g(2).h(3).d(4).a(5).b(6);
|
||||
|
||||
let mut c = C::new();
|
||||
c.a(1).b(2).c(3).d(4).e(5).f(6).g(7).h(8);
|
||||
assert_dict_c(&c);
|
||||
assert_dict_c2(c.clone());
|
||||
assert_dict_c3(Some(&c));
|
||||
assert_dict_c4(Some(c));
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn get_dict() {
|
||||
mk_dict_a();
|
||||
assert!(mk_dict_a2().is_some());
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn casing() {
|
||||
CamelCaseMe::new().snake_case_me(3);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn many_types() {
|
||||
ManyTypes::new()
|
||||
.a("a");
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn required() {
|
||||
assert_dict_required(Required::new(3, "a").c(4));
|
||||
}
|
47
crates/webidl-tests/dictionary.webidl
vendored
Normal file
47
crates/webidl-tests/dictionary.webidl
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
// example from https://heycam.github.io/webidl/#idl-dictionaries
|
||||
dictionary B : A {
|
||||
long b;
|
||||
long a;
|
||||
};
|
||||
|
||||
dictionary A {
|
||||
long c;
|
||||
long g;
|
||||
};
|
||||
|
||||
dictionary C : B {
|
||||
long e;
|
||||
long f;
|
||||
};
|
||||
|
||||
partial dictionary A {
|
||||
long h;
|
||||
long d;
|
||||
};
|
||||
|
||||
// case needs changing
|
||||
dictionary camel_case_me {
|
||||
long snakeCaseMe;
|
||||
};
|
||||
|
||||
dictionary ManyTypes {
|
||||
DOMString a;
|
||||
octet n1;
|
||||
byte n2;
|
||||
unsigned short n3;
|
||||
short n4;
|
||||
unsigned long n5;
|
||||
long n6;
|
||||
// TODO: needs fixing
|
||||
// OtherDict c;
|
||||
};
|
||||
|
||||
dictionary OtherDict {
|
||||
long a;
|
||||
};
|
||||
|
||||
dictionary Required {
|
||||
required DOMString b;
|
||||
required long a;
|
||||
long c;
|
||||
};
|
@ -11,3 +11,4 @@ pub mod enums;
|
||||
pub mod namespace;
|
||||
pub mod simple;
|
||||
pub mod throws;
|
||||
pub mod dictionary;
|
||||
|
Reference in New Issue
Block a user