Migrate wasm-bindgen to using walrus

This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.

The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.

Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)

It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.

Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!

Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
This commit is contained in:
Alex Crichton
2019-01-31 09:54:23 -08:00
parent c30dbc3179
commit 894b479213
74 changed files with 1007 additions and 3522 deletions

View File

@ -37,7 +37,7 @@ pub(crate) struct FirstPassRecord<'src> {
pub(crate) dictionaries: BTreeMap<&'src str, DictionaryData<'src>>,
pub(crate) callbacks: BTreeSet<&'src str>,
pub(crate) callback_interfaces: BTreeMap<&'src str, CallbackInterfaceData<'src>>,
pub(crate) immutable_f32_whitelist: BTreeSet<&'static str>
pub(crate) immutable_f32_whitelist: BTreeSet<&'static str>,
}
/// We need to collect interface data during the first pass, to be used later.

View File

@ -43,7 +43,7 @@ pub(crate) enum IdlType<'a> {
Uint32Array,
Float32Array {
/// Whether or not the generated web-sys function should use an immutable slice
immutable: bool
immutable: bool,
},
Float64Array,
ArrayBufferView,
@ -332,7 +332,7 @@ impl<'a> ToIdlType<'a> for Identifier<'a> {
// instead use the immutable version.
impl<'a> ToIdlType<'a> for term::Float32Array {
fn to_idl_type(&self, _record: &FirstPassRecord<'a>) -> IdlType<'a> {
IdlType::Float32Array {immutable: false}
IdlType::Float32Array { immutable: false }
}
}
@ -520,7 +520,7 @@ impl<'a> IdlType<'a> {
IdlType::Uint16Array => Some(array("u16", pos, false)),
IdlType::Int32Array => Some(array("i32", pos, false)),
IdlType::Uint32Array => Some(array("u32", pos, false)),
IdlType::Float32Array {immutable} => Some(array("f32", pos, *immutable)),
IdlType::Float32Array { immutable } => Some(array("f32", pos, *immutable)),
IdlType::Float64Array => Some(array("f64", pos, false)),
IdlType::ArrayBufferView | IdlType::BufferSource => js_sys("Object"),

View File

@ -182,23 +182,21 @@ fn builtin_idents() -> BTreeSet<Ident> {
}
fn immutable_f32_whitelist() -> BTreeSet<&'static str> {
BTreeSet::from_iter(
vec![
// WebGlRenderingContext
"uniform1fv",
"uniform2fv",
"uniform3fv",
"uniform4fv",
"uniformMatrix2fv",
"uniformMatrix3fv",
"uniformMatrix4fv",
"vertexAttrib1fv",
"vertexAttrib2fv",
"vertexAttrib3fv",
"vertexAttrib4fv",
// TODO: Add another type's functions here. Leave a comment header with the type name
]
)
BTreeSet::from_iter(vec![
// WebGlRenderingContext
"uniform1fv",
"uniform2fv",
"uniform3fv",
"uniform4fv",
"uniformMatrix2fv",
"uniformMatrix3fv",
"uniformMatrix4fv",
"vertexAttrib1fv",
"vertexAttrib2fv",
"vertexAttrib3fv",
"vertexAttrib4fv",
// TODO: Add another type's functions here. Leave a comment header with the type name
])
}
/// Run codegen on the AST to generate rust code.

View File

@ -631,7 +631,6 @@ impl<'src> FirstPassRecord<'src> {
return ret;
}
/// When generating our web_sys APIs we default to setting slice references that
/// get passed to JS as mutable in case they get mutated in JS.
///
@ -645,7 +644,7 @@ impl<'src> FirstPassRecord<'src> {
fn maybe_adjust<'a>(&self, mut idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> {
let op = match id {
OperationId::Operation(Some(op)) => op,
_ => return idl_type
_ => return idl_type,
};
if self.immutable_f32_whitelist.contains(op) {
@ -656,8 +655,6 @@ impl<'src> FirstPassRecord<'src> {
idl_type
}
}
/// Search for an attribute by name in some webidl object's attributes.
@ -721,8 +718,7 @@ pub fn is_structural(
// Note that once host bindings is implemented we'll want to switch this
// from `true` to `false`, and then we'll want to largely read information
// from the WebIDL about whether to use structural bindings or not.
true
|| has_named_attribute(item_attrs, "Unforgeable")
true || has_named_attribute(item_attrs, "Unforgeable")
|| has_named_attribute(container_attrs, "Unforgeable")
|| has_ident_attribute(container_attrs, "Global")
}
@ -749,9 +745,11 @@ fn flag_slices_immutable(ty: &mut IdlType) {
IdlType::Record(item1, item2) => {
flag_slices_immutable(item1);
flag_slices_immutable(item2);
},
}
IdlType::Union(list) => {
for item in list { flag_slices_immutable(item); }
for item in list {
flag_slices_immutable(item);
}
}
// catch-all for everything else like Object
_ => {}