1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-07-19 14:11:58 +00:00

Start removal of vector special-casing

This commit starts wasm-bindgen down a path of removing the special
casing it currently has around vectors, slices, and strings. This has
long been a thorn in wasm-bindgen's side as it doesn't handle other
kinds of vectors and otherwise is very inflexible with future additions.
Additionally it leads to a lot of duplicated-ish code throughout various
portions of codegen.

The fundamental reason for this was that two arguments were required to
be passed back to wasm, and I couldn't figure out a way to shove both
those arguments into a function argument. The new strategy here is that
there is one global stack well known to both JS and Rust which arguments
*may* also be transferred between.

By default all ABI arguments pass as literal function arguments, but if
two or more arguments need to be passed then the extra ones are all
passed through this global stack. The stack is effectively temporary
scratch space when crossing the JS/Rust boundary (both ways). No long
term storage is intended here.

The `simple` test is passing as a result of this commit, using strings
internally. The `Vector` type in the AST has been removed (yay!) and the
bulk of the implementation of slices and vectors now resides in the
`wasm-bindgen` crate itself, defining how to pass all these arguments
around. The JS generator, however, still needs to know about all the
sorts of vectors so it can generate appropriate code for JS.

Future commits will continue cleanup and get the rest of the tests
working.
This commit is contained in:
Alex Crichton
2018-03-31 07:57:47 -07:00
parent 25af16c7d9
commit cdbb31f3a9
8 changed files with 622 additions and 491 deletions
crates
backend
cli-support
shared
src

@@ -82,9 +82,6 @@ pub struct Variant {
}
pub enum Type {
// special
Vector(VectorType, bool),
ByRef(syn::Type),
ByMutRef(syn::Type),
ByValue(syn::Type),
@@ -320,7 +317,6 @@ impl Program {
let class = match *class {
Type::ByRef(ref t) | Type::ByValue(ref t) => t,
Type::ByMutRef(_) => panic!("first method argument cannot be mutable ref"),
Type::Vector(..) => panic!("method receivers cannot be vectors"),
};
let class_name = match *class {
syn::Type::Path(syn::TypePath {
@@ -505,61 +501,13 @@ pub fn extract_path_ident(path: &syn::Path) -> Option<syn::Ident> {
impl Type {
pub fn from(ty: &syn::Type) -> Type {
match *ty {
syn::Type::Reference(ref r) => {
match *r.elem {
syn::Type::Path(syn::TypePath {
qself: None,
ref path,
}) => {
let ident = extract_path_ident(path);
match ident.as_ref().map(|s| s.as_ref()) {
Some("str") => return Type::Vector(VectorType::String, false),
_ => {}
}
}
syn::Type::Slice(ref slice) => {
if let Some(ty) = VectorType::from(&slice.elem) {
return Type::Vector(ty, false);
}
}
_ => {}
}
return if r.mutability.is_some() {
Type::ByMutRef((*r.elem).clone())
} else {
Type::ByRef((*r.elem).clone())
};
if let syn::Type::Reference(ref r) = *ty {
return if r.mutability.is_some() {
Type::ByMutRef((*r.elem).clone())
} else {
Type::ByRef((*r.elem).clone())
}
syn::Type::Path(syn::TypePath {
qself: None,
ref path,
}) if path.leading_colon.is_none() && path.segments.len() == 1 =>
{
let seg = path.segments.first().unwrap().into_value();
match seg.arguments {
syn::PathArguments::None => match seg.ident.as_ref() {
"String" => return Type::Vector(VectorType::String, true),
_ => {}
},
syn::PathArguments::AngleBracketed(ref t)
if seg.ident == "Vec" && t.args.len() == 1 =>
{
match **t.args.first().unwrap().value() {
syn::GenericArgument::Type(ref t) => {
if let Some(ty) = VectorType::from(t) {
return Type::Vector(ty, true);
}
}
_ => {}
}
}
_ => {}
}
}
_ => {}
}
Type::ByValue(ty.clone())
}
}