Overhaul how type information gets to the CLI

This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.

Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.

Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.

This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes

A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.

This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
This commit is contained in:
Alex Crichton
2018-04-13 07:33:46 -07:00
parent eb9a6524b9
commit 3305621012
15 changed files with 1192 additions and 921 deletions

View File

@ -1,5 +1,3 @@
use literal::{self, Literal};
use proc_macro2::Span;
use quote::{ToTokens, Tokens};
use shared;
use syn;
@ -404,22 +402,14 @@ impl Program {
})
}
pub fn literal(&self, dst: &mut Tokens) -> usize {
let mut tmp = Tokens::new();
let cnt = {
let mut a = literal::LiteralBuilder::new(&mut tmp);
Literal::literal(self, &mut a);
a.finish()
};
let cnt = cnt as u32;
(quote! {
(#cnt >> 0) as u8,
(#cnt >> 8) as u8,
(#cnt >> 16) as u8,
(#cnt >> 24) as u8
}).to_tokens(dst);
tmp.to_tokens(dst);
(cnt as usize) + 4
pub fn shared(&self) -> shared::Program {
shared::Program {
exports: self.exports.iter().map(|a| a.shared()).collect(),
enums: self.enums.iter().map(|a| a.shared()).collect(),
imports: self.imports.iter().map(|a| a.shared()).collect(),
version: shared::version(),
schema_version: shared::SCHEMA_VERSION.to_string(),
}
}
}
@ -511,6 +501,12 @@ impl Function {
mutable,
)
}
fn shared(&self) -> shared::Function {
shared::Function {
name: self.name.as_ref().to_string(),
}
}
}
pub fn extract_path_ident(path: &syn::Path) -> Option<syn::Ident> {
@ -539,14 +535,59 @@ impl Export {
syn::Ident::from(generated_name)
}
pub fn export_name(&self) -> syn::LitStr {
let name = match self.class {
pub fn export_name(&self) -> String {
match self.class {
Some(class) => {
shared::struct_function_export_name(class.as_ref(), self.function.name.as_ref())
}
None => shared::free_function_export_name(self.function.name.as_ref()),
};
syn::LitStr::new(&name, Span::call_site())
}
}
fn shared(&self) -> shared::Export {
shared::Export {
class: self.class.map(|s| s.as_ref().to_string()),
method: self.method,
function: self.function.shared(),
}
}
}
impl Enum {
fn shared(&self) -> shared::Enum {
shared::Enum {
name: self.name.as_ref().to_string(),
variants: self.variants.iter().map(|v| v.shared()).collect(),
}
}
}
impl Variant {
fn shared(&self) -> shared::EnumVariant {
shared::EnumVariant {
name: self.name.as_ref().to_string(),
value: self.value,
}
}
}
impl Import {
fn shared(&self) -> shared::Import {
shared::Import {
module: self.module.clone(),
js_namespace: self.js_namespace.map(|s| s.as_ref().to_string()),
kind: self.kind.shared(),
}
}
}
impl ImportKind {
fn shared(&self) -> shared::ImportKind {
match *self {
ImportKind::Function(ref f) => shared::ImportKind::Function(f.shared()),
ImportKind::Static(ref f) => shared::ImportKind::Static(f.shared()),
ImportKind::Type(ref f) => shared::ImportKind::Type(f.shared()),
}
}
}
@ -560,6 +601,60 @@ impl ImportFunction {
assert!(name.starts_with("set_"), "setters must start with `set_`");
name[4..].to_string()
}
fn shared(&self) -> shared::ImportFunction {
let mut method = false;
let mut js_new = false;
let mut class_name = None;
match self.kind {
ImportFunctionKind::Method { ref class, .. } => {
method = true;
class_name = Some(class);
}
ImportFunctionKind::JsConstructor { ref class, .. } => {
js_new = true;
class_name = Some(class);
}
ImportFunctionKind::Normal => {}
}
let mut getter = None;
let mut setter = None;
if let Some(s) = self.function.opts.getter() {
let s = s.map(|s| s.to_string());
getter = Some(s.unwrap_or_else(|| self.infer_getter_property()));
}
if let Some(s) = self.function.opts.setter() {
let s = s.map(|s| s.to_string());
setter = Some(s.unwrap_or_else(|| self.infer_setter_property()));
}
shared::ImportFunction {
shim: self.shim.as_ref().to_string(),
catch: self.function.opts.catch(),
method,
js_new,
structural: self.function.opts.structural(),
getter,
setter,
class: class_name.cloned(),
function: self.function.shared(),
}
}
}
impl ImportStatic {
fn shared(&self) -> shared::ImportStatic {
shared::ImportStatic {
name: self.js_name.as_ref().to_string(),
shim: self.shim.as_ref().to_string(),
}
}
}
impl ImportType {
fn shared(&self) -> shared::ImportType {
shared::ImportType { }
}
}
impl Struct {