Second large refactor for WebIDL bindings

This commit is the second, and hopefully last massive, refactor for
using WebIDL bindings internally in `wasm-bindgen`. This commit actually
fully executes on the task at hand, moving `wasm-bindgen` to internally
using WebIDL bindings throughout its code generation, anyref passes,
etc. This actually fixes a number of issues that have existed in the
anyref pass for some time now!

The main changes here are to basically remove the usage of `Descriptor`
from generating JS bindings. Instead two new types are introduced:
`NonstandardIncoming` and `NonstandardOutgoing` which are bindings lists
used for incoming/outgoing bindings. These mirror the standard
terminology and literally have variants which are the standard values.
All `Descriptor` types are now mapped into lists of incoming/outgoing
bindings and used for process in wasm-bindgen. All JS generation has
been refactored and updated to now process these lists of bindings
instead of the previous `Descriptor`.

In other words this commit takes `js2rust.rs` and `rust2js.rs` and first
splits them in two. Interpretation of `Descriptor` and what to do for
conversions is in the binding selection modules. The actual generation
of JS from the binding selection is now performed by `incoming.rs` and
`outgoing.rs`. To boot this also deduplicates all the code between the
argument handling of `js2rust.rs` and return value handling of
`rust2js.rs`. This means that to implement a new binding you only need
to implement it one place and it's implemented for free in the other!

This commit is not the end of the story though. I would like to add a
mdoe to `wasm-bindgen` that literally emits a WebIDL bindings section.
That's left for a third (and hopefully final) refactoring which is also
intended to optimize generated JS for bindings.

This commit currently loses the optimization where an imported is hooked
up by value directly whenever a shim isn't needed. It's planned that
the next refactoring to emit a webidl binding section that can be added
back in. It shouldn't be too too hard hopefully since all the
scaffolding is in place now.

cc #1524
This commit is contained in:
Alex Crichton
2019-06-10 07:09:51 -07:00
parent a1fc270f2a
commit 3cc30843e3
16 changed files with 3986 additions and 2541 deletions

View File

@ -2,5 +2,319 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add(a: u32, b: u32) -> u32 {
a + b
lol as u32
}
#[wasm_bindgen]
pub enum Enum {
A,
B,
}
#[wasm_bindgen]
pub struct Rust {}
#[wasm_bindgen]
pub fn wut(
// anyref
_: &JsValue,
_: JsValue,
// rust
_: &Rust,
_: Rust,
_: Enum,
_: bool,
_: char,
// numbers
_: f32,
_: f64,
_: i8,
_: u8,
_: i16,
_: u16,
_: i32,
_: u32,
_: i64,
_: u64,
// slices
_: &[u8],
_: &[i8],
_: &[u16],
_: &[i16],
_: &[u32],
_: &[i32],
_: &[u64],
_: &[i64],
_: &[f32],
_: &[f64],
// vectors
_: Vec<u8>,
_: Vec<i8>,
_: Vec<u16>,
_: Vec<i16>,
_: Vec<u32>,
_: Vec<i32>,
_: Vec<u64>,
_: Vec<i64>,
_: Vec<f32>,
_: Vec<f64>,
// option float
_: Option<f32>,
_: Option<f64>,
// option integer
_: Option<i8>,
_: Option<u8>,
_: Option<i16>,
_: Option<u16>,
_: Option<i32>,
_: Option<u32>,
_: Option<i64>,
_: Option<u64>,
// option misc
_: Option<bool>,
_: Option<char>,
_: Option<Enum>,
_: Option<Rust>,
// option vectors
_: Option<Vec<u8>>,
_: Option<Vec<i8>>,
_: Option<Vec<u16>>,
_: Option<Vec<i16>>,
_: Option<Vec<u32>>,
_: Option<Vec<i32>>,
_: Option<Vec<u64>>,
_: Option<Vec<i64>>,
_: Option<Vec<f32>>,
_: Option<Vec<f64>>,
) {
}
#[wasm_bindgen]
pub fn goo(x: u32) {
unsafe {
std::mem::transmute::<u32, fn()>(x)();
}
}
#[wasm_bindgen]
pub fn r1() -> Rust {
loop {}
}
#[wasm_bindgen]
pub fn r2() -> Vec<u32> {
loop {}
}
#[wasm_bindgen]
pub fn r3() -> JsValue {
loop {}
}
#[wasm_bindgen]
pub fn r4() -> i8 {
loop {}
}
#[wasm_bindgen]
pub fn r5() -> u8 {
loop {}
}
#[wasm_bindgen]
pub fn r6() -> i16 {
loop {}
}
#[wasm_bindgen]
pub fn r7() -> u16 {
loop {}
}
#[wasm_bindgen]
pub fn r8() -> i32 {
loop {}
}
#[wasm_bindgen]
pub fn r9() -> u32 {
loop {}
}
#[wasm_bindgen]
pub fn r10() -> i64 {
loop {}
}
#[wasm_bindgen]
pub fn r11() -> u64 {
loop {}
}
#[wasm_bindgen]
pub fn r12() -> f32 {
loop {}
}
#[wasm_bindgen]
pub fn r13() -> f64 {
loop {}
}
#[wasm_bindgen]
pub fn r14() -> bool {
loop {}
}
#[wasm_bindgen]
pub fn r15() -> char {
loop {}
}
#[wasm_bindgen]
pub fn r16() -> Enum {
loop {}
}
#[wasm_bindgen]
pub fn r17() -> Option<Vec<u32>> {
loop {}
}
#[wasm_bindgen]
pub fn r18() -> Option<i32> {
loop {}
}
#[wasm_bindgen]
pub fn r19() -> Option<bool> {
loop {}
}
#[wasm_bindgen]
pub fn r20() -> Option<char> {
loop {}
}
#[wasm_bindgen]
pub fn r21() -> Option<Enum> {
loop {}
}
#[wasm_bindgen]
pub fn r22() -> Option<Rust> {
loop {}
}
#[wasm_bindgen]
extern "C" {
pub fn lol(
// anyref
_: &JsValue,
_: JsValue,
// rust
// _: &Rust,
_: Rust,
_: Enum,
_: bool,
_: char,
// numbers
_: f32,
_: f64,
_: i8,
_: u8,
_: i16,
_: u16,
_: i32,
_: u32,
_: i64,
_: u64,
// slices
_: &[u8],
_: &[i8],
_: &[u16],
_: &[i16],
_: &[u32],
_: &[i32],
_: &[u64],
_: &[i64],
_: &[f32],
_: &[f64],
// vectors
_: Vec<u8>,
_: Vec<i8>,
_: Vec<u16>,
_: Vec<i16>,
_: Vec<u32>,
_: Vec<i32>,
_: Vec<u64>,
_: Vec<i64>,
_: Vec<f32>,
_: Vec<f64>,
// option float
_: Option<f32>,
_: Option<f64>,
// option integer
_: Option<i8>,
_: Option<u8>,
_: Option<i16>,
_: Option<u16>,
_: Option<i32>,
_: Option<u32>,
_: Option<i64>,
_: Option<u64>,
// option misc
_: Option<bool>,
_: Option<char>,
_: Option<Enum>,
_: Option<Rust>,
// option vectors
_: Option<Vec<u8>>,
_: Option<Vec<i8>>,
_: Option<Vec<u16>>,
_: Option<Vec<i16>>,
_: Option<Vec<u32>>,
_: Option<Vec<i32>>,
_: Option<Vec<u64>>,
_: Option<Vec<i64>>,
_: Option<Vec<f32>>,
_: Option<Vec<f64>>,
// option slices
_: Option<&[u8]>,
_: Option<&[i8]>,
_: Option<&[u16]>,
_: Option<&[i16]>,
_: Option<&[u32]>,
_: Option<&[i32]>,
_: Option<&[u64]>,
_: Option<&[i64]>,
_: Option<&[f32]>,
_: Option<&[f64]>,
// closures
_: &dyn Fn(),
_: &mut dyn FnMut(),
_: &Closure<dyn Fn()>,
_: &Closure<dyn FnMut()>,
);
}
macro_rules! t {
($($n:ident : $t:ty,)*) => (
$(
#[wasm_bindgen]
pub fn $n() -> u32 {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = nowhere)]
fn $n() -> $t;
}
return $n as u32;
}
)*
)
}
t! {
x1: i8,
x2: u8,
x3: i16,
x4: u16,
x5: i32,
x6: u32,
x7: i64,
x8: u64,
x9: f32,
x10: f64,
x11: Rust,
x12: Vec<u32>,
x13: JsValue,
x14: bool,
x15: char,
x16: Enum,
x17: Option<Vec<u32>>,
x18: Option<i32>,
x19: Option<char>,
x20: Option<bool>,
x21: Option<Rust>,
x22: Option<Enum>,
}