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

View File

@ -166,9 +166,6 @@ fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
let version = shared::version();
let mut ret = Vec::new();
#[repr(packed)]
struct Unaligned(u32);
module.sections_mut().retain(|s| {
let custom = match *s {
Section::Custom(ref s) => s,
@ -178,21 +175,16 @@ fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
return true
}
assert!(custom.payload().len() % 4 == 0);
let mut payload = unsafe {
slice::from_raw_parts(custom.payload().as_ptr() as *const Unaligned,
custom.payload().len() / 4)
};
let mut payload = custom.payload();
while payload.len() > 0 {
let len = payload[0].0.to_le();
assert!(len % 4 == 0);
let (a, b) = payload[1..].split_at((len / 4) as usize);
let len =
((payload[0] as usize) << 0) |
((payload[1] as usize) << 8) |
((payload[2] as usize) << 16) |
((payload[3] as usize) << 24);
let (a, b) = payload[4..].split_at(len as usize);
payload = b;
let json = a.iter()
.map(|i| char::from_u32(i.0.to_le()).unwrap())
.collect::<String>();
let p: shared::Program = match serde_json::from_str(&json) {
let p: shared::Program = match serde_json::from_slice(&a) {
Ok(f) => f,
Err(e) => {
panic!("failed to decode what looked like wasm-bindgen data: {}", e)