2019-12-03 11:16:44 -06:00
|
|
|
//! Definition of how to convert Rust types (`Description`) into wasm types
|
|
|
|
//! through adapter functions.
|
|
|
|
//!
|
|
|
|
//! Note that many Rust types use "nonstandard" instructions which only work in
|
|
|
|
//! the JS output, not for the "pure wasm interface types" output.
|
|
|
|
//!
|
|
|
|
//! Note that the mirror operation, going from WebAssembly to JS, is found in
|
|
|
|
//! the `outgoing.rs` module.
|
|
|
|
|
|
|
|
use crate::descriptor::Descriptor;
|
|
|
|
use crate::wit::InstructionData;
|
|
|
|
use crate::wit::{AdapterType, Instruction, InstructionBuilder, StackChange};
|
|
|
|
use anyhow::{bail, format_err, Error};
|
|
|
|
use walrus::ValType;
|
|
|
|
|
|
|
|
impl InstructionBuilder<'_, '_> {
|
|
|
|
/// Process a `Descriptor` as if it's being passed from JS to Rust. This
|
|
|
|
/// will skip `Unit` and otherwise internally add instructions necessary to
|
|
|
|
/// convert the foreign type into the Rust bits.
|
|
|
|
pub fn incoming(&mut self, arg: &Descriptor) -> Result<(), Error> {
|
|
|
|
if let Descriptor::Unit = arg {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// This is a wrapper around `_incoming` to have a number of sanity checks
|
|
|
|
// that we don't forget things. We should always produce at least one
|
|
|
|
// wasm arge and exactly one webidl arg. Additionally the number of
|
|
|
|
// bindings should always match the number of webidl types for now.
|
|
|
|
let input_before = self.input.len();
|
|
|
|
let output_before = self.output.len();
|
|
|
|
self._incoming(arg)?;
|
|
|
|
assert_eq!(
|
|
|
|
input_before + 1,
|
|
|
|
self.input.len(),
|
|
|
|
"didn't push an input {:?}",
|
|
|
|
arg
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
output_before < self.output.len(),
|
|
|
|
"didn't push more outputs {:?}",
|
|
|
|
arg
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _incoming(&mut self, arg: &Descriptor) -> Result<(), Error> {
|
|
|
|
use walrus::ValType as WasmVT;
|
|
|
|
use wit_walrus::ValType as WitVT;
|
|
|
|
match arg {
|
|
|
|
Descriptor::Boolean => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::Bool],
|
|
|
|
Instruction::I32FromBool,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Char => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::String],
|
|
|
|
Instruction::I32FromStringFirstChar,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Anyref => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::Anyref],
|
|
|
|
Instruction::I32FromAnyrefOwned,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
2020-02-20 01:14:32 +10:00
|
|
|
Descriptor::NamedAnyref(name) => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::NamedAnyref(name.clone())],
|
|
|
|
Instruction::I32FromAnyrefOwned,
|
|
|
|
&[AdapterType::I32]
|
|
|
|
)
|
|
|
|
}
|
2019-12-03 11:16:44 -06:00
|
|
|
Descriptor::RustStruct(class) => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::Struct(class.clone())],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromAnyrefRustOwned {
|
|
|
|
class: class.clone(),
|
|
|
|
},
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::I8 => self.number(WitVT::S8, WasmVT::I32),
|
|
|
|
Descriptor::U8 => self.number(WitVT::U8, WasmVT::I32),
|
|
|
|
Descriptor::I16 => self.number(WitVT::S16, WasmVT::I32),
|
|
|
|
Descriptor::U16 => self.number(WitVT::U16, WasmVT::I32),
|
|
|
|
Descriptor::I32 => self.number(WitVT::S32, WasmVT::I32),
|
|
|
|
Descriptor::U32 => self.number(WitVT::U32, WasmVT::I32),
|
|
|
|
Descriptor::I64 => self.number64(true),
|
|
|
|
Descriptor::U64 => self.number64(false),
|
|
|
|
Descriptor::F32 => {
|
|
|
|
self.get(AdapterType::F32);
|
|
|
|
self.output.push(AdapterType::F32);
|
|
|
|
}
|
|
|
|
Descriptor::F64 => {
|
|
|
|
self.get(AdapterType::F64);
|
|
|
|
self.output.push(AdapterType::F64);
|
|
|
|
}
|
|
|
|
Descriptor::Enum { .. } => self.number(WitVT::U32, WasmVT::I32),
|
|
|
|
Descriptor::Ref(d) => self.incoming_ref(false, d)?,
|
|
|
|
Descriptor::RefMut(d) => self.incoming_ref(true, d)?,
|
|
|
|
Descriptor::Option(d) => self.incoming_option(d)?,
|
|
|
|
|
|
|
|
Descriptor::String | Descriptor::CachedString => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::String],
|
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations
This commit starts adding a test suite which checks in, to the
repository, test assertions for both the JS and wasm file outputs of a
Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be
exhaustive or large scale tests, but rather micro-tests to help observe
the changes in `wasm-bindgen`'s output over time.
The motivation for this commit is basically overhauling how all the GC
passes work in `wasm-bindgen` today. The reorganization is also included
in this commit as well.
Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes
of `walrus` in a bunch of places to ensure that less "garbage" was seen
by future passes. This not only was a source of slowdown but it also was
pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked
through.
The strategy taken in this commit is to have one precise location for a
GC pass, and everything goes through there. This is achieved by:
* All internal exports are removed immediately when generating the
nonstandard wasm interface types section. Internal exports,
intrinsics, and runtime support are all referenced by the various
instructions and/or sections that use them. This means that we now
have precise tracking of what an adapter uses.
* This in turn enables us to implement the `add_gc_roots` function for
`walrus` custom sections, which in turn allows walrus GC passes to do
what `unexport_unused_intrinsics` did before. That function is now no
longer necessary, but effectively works the same way. All intrinsics
are unexported at the beginning and then they're selectively
re-imported and re-exported through the JS glue generation pass as
necessary and defined by the bindings.
* Passes like the `anyref` pass are now much more precise about the
intrinsics that they work with. The `anyref` pass also deletes any
internal intrinsics found and also does some rewriting of the adapters
aftewards now to hook up calls to the heap count import to the heap
count intrinsic in the wasm module.
* Fix handling of __wbindgen_realloc
The final user of the `require_internal_export` function was
`__wbindgen_realloc`. This usage has now been removed by updating how we
handle usage of the `realloc` function.
The wasm interface types standard doesn't have a `realloc` function
slot, nor do I think it ever will. This means that as a polyfill for
wasm interface types we'll always have to support the lack of `realloc`.
For direct Rust to JS, however, we can still optionally handle
`realloc`. This is all handled with a few internal changes.
* Custom `StringToMemory` instructions now exist. These have an extra
`realloc` slot to store an intrinsic, if found.
* Our custom instructions are lowered to the standard instructions when
generating an interface types section.
* The `realloc` function, if present, is passed as an argument like the
malloc function when passing strings to wasm. If it's not present we
use a slower fallback, but if it's present we use the faster
implementation.
This should mean that there's little-to-no impact on existing users of
`wasm-bindgen`, but this should continue to still work for wasm
interface types polyfills and such. Additionally the GC passes now work
in that they don't delete `__wbindgen_realloc` which we later try to
reference.
* Add an empty test for the anyref pass
* Precisely track I32FromOptionAnyref's dependencies
This depends on the anyref table and a function to allocate an index if
the anyref pass is running, so be sure to track that in the instruction
itself for GC rooting.
* Trim extraneous exports from nop anyref module
Or if you're otherwise not using anyref slices, don't force some
intrinsics to exist.
* Remove globals from reference tests
Looks like these values adjust in slight but insignificant ways over
time
* Update the anyref xform tests
2019-12-04 12:01:39 -06:00
|
|
|
Instruction::StringToMemory {
|
|
|
|
malloc: self.cx.malloc()?,
|
|
|
|
realloc: self.cx.realloc(),
|
|
|
|
mem: self.cx.memory()?,
|
|
|
|
},
|
2019-12-03 11:16:44 -06:00
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Descriptor::Vector(_) => {
|
|
|
|
let kind = arg.vector_kind().ok_or_else(|| {
|
|
|
|
format_err!("unsupported argument type for calling Rust function from JS {:?}", arg)
|
|
|
|
})?;
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::Vector(kind)],
|
|
|
|
Instruction::VectorToMemory {
|
|
|
|
kind,
|
|
|
|
malloc: self.cx.malloc()?,
|
|
|
|
mem: self.cx.memory()?,
|
|
|
|
},
|
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't be passed from JS to Rust yet
|
|
|
|
Descriptor::Function(_) |
|
|
|
|
Descriptor::Closure(_) |
|
|
|
|
|
|
|
|
// Always behind a `Ref`
|
|
|
|
Descriptor::Slice(_) => bail!(
|
|
|
|
"unsupported argument type for calling Rust function from JS: {:?}",
|
|
|
|
arg
|
|
|
|
),
|
|
|
|
|
|
|
|
// nothing to do
|
|
|
|
Descriptor::Unit => {}
|
|
|
|
|
|
|
|
// Largely synthetic and can't show up
|
|
|
|
Descriptor::ClampedU8 => unreachable!(),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn incoming_ref(&mut self, mutable: bool, arg: &Descriptor) -> Result<(), Error> {
|
|
|
|
match arg {
|
|
|
|
Descriptor::RustStruct(class) => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::Struct(class.clone())],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromAnyrefRustBorrow {
|
|
|
|
class: class.clone(),
|
|
|
|
},
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Anyref => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::Anyref],
|
|
|
|
Instruction::I32FromAnyrefBorrow,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
2020-02-20 01:14:32 +10:00
|
|
|
Descriptor::NamedAnyref(name) => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::NamedAnyref(name.clone())],
|
|
|
|
Instruction::I32FromAnyrefBorrow,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
2019-12-03 11:16:44 -06:00
|
|
|
Descriptor::String | Descriptor::CachedString => {
|
|
|
|
// This allocation is cleaned up once it's received in Rust.
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::String],
|
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations
This commit starts adding a test suite which checks in, to the
repository, test assertions for both the JS and wasm file outputs of a
Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be
exhaustive or large scale tests, but rather micro-tests to help observe
the changes in `wasm-bindgen`'s output over time.
The motivation for this commit is basically overhauling how all the GC
passes work in `wasm-bindgen` today. The reorganization is also included
in this commit as well.
Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes
of `walrus` in a bunch of places to ensure that less "garbage" was seen
by future passes. This not only was a source of slowdown but it also was
pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked
through.
The strategy taken in this commit is to have one precise location for a
GC pass, and everything goes through there. This is achieved by:
* All internal exports are removed immediately when generating the
nonstandard wasm interface types section. Internal exports,
intrinsics, and runtime support are all referenced by the various
instructions and/or sections that use them. This means that we now
have precise tracking of what an adapter uses.
* This in turn enables us to implement the `add_gc_roots` function for
`walrus` custom sections, which in turn allows walrus GC passes to do
what `unexport_unused_intrinsics` did before. That function is now no
longer necessary, but effectively works the same way. All intrinsics
are unexported at the beginning and then they're selectively
re-imported and re-exported through the JS glue generation pass as
necessary and defined by the bindings.
* Passes like the `anyref` pass are now much more precise about the
intrinsics that they work with. The `anyref` pass also deletes any
internal intrinsics found and also does some rewriting of the adapters
aftewards now to hook up calls to the heap count import to the heap
count intrinsic in the wasm module.
* Fix handling of __wbindgen_realloc
The final user of the `require_internal_export` function was
`__wbindgen_realloc`. This usage has now been removed by updating how we
handle usage of the `realloc` function.
The wasm interface types standard doesn't have a `realloc` function
slot, nor do I think it ever will. This means that as a polyfill for
wasm interface types we'll always have to support the lack of `realloc`.
For direct Rust to JS, however, we can still optionally handle
`realloc`. This is all handled with a few internal changes.
* Custom `StringToMemory` instructions now exist. These have an extra
`realloc` slot to store an intrinsic, if found.
* Our custom instructions are lowered to the standard instructions when
generating an interface types section.
* The `realloc` function, if present, is passed as an argument like the
malloc function when passing strings to wasm. If it's not present we
use a slower fallback, but if it's present we use the faster
implementation.
This should mean that there's little-to-no impact on existing users of
`wasm-bindgen`, but this should continue to still work for wasm
interface types polyfills and such. Additionally the GC passes now work
in that they don't delete `__wbindgen_realloc` which we later try to
reference.
* Add an empty test for the anyref pass
* Precisely track I32FromOptionAnyref's dependencies
This depends on the anyref table and a function to allocate an index if
the anyref pass is running, so be sure to track that in the instruction
itself for GC rooting.
* Trim extraneous exports from nop anyref module
Or if you're otherwise not using anyref slices, don't force some
intrinsics to exist.
* Remove globals from reference tests
Looks like these values adjust in slight but insignificant ways over
time
* Update the anyref xform tests
2019-12-04 12:01:39 -06:00
|
|
|
Instruction::StringToMemory {
|
|
|
|
malloc: self.cx.malloc()?,
|
|
|
|
realloc: self.cx.realloc(),
|
|
|
|
mem: self.cx.memory()?,
|
|
|
|
},
|
2019-12-03 11:16:44 -06:00
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Slice(_) => {
|
|
|
|
// like strings, this allocation is cleaned up after being
|
|
|
|
// received in Rust.
|
|
|
|
let kind = arg.vector_kind().ok_or_else(|| {
|
|
|
|
format_err!(
|
|
|
|
"unsupported argument type for calling Rust function from JS {:?}",
|
|
|
|
arg
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
if mutable {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::Vector(kind)],
|
|
|
|
Instruction::MutableSliceToMemory {
|
|
|
|
kind,
|
|
|
|
malloc: self.cx.malloc()?,
|
|
|
|
mem: self.cx.memory()?,
|
|
|
|
free: self.cx.free()?,
|
|
|
|
},
|
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::Vector(kind)],
|
|
|
|
Instruction::VectorToMemory {
|
|
|
|
kind,
|
|
|
|
malloc: self.cx.malloc()?,
|
|
|
|
mem: self.cx.memory()?,
|
|
|
|
},
|
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => bail!(
|
|
|
|
"unsupported reference argument type for calling Rust function from JS: {:?}",
|
|
|
|
arg
|
|
|
|
),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn incoming_option(&mut self, arg: &Descriptor) -> Result<(), Error> {
|
|
|
|
match arg {
|
|
|
|
Descriptor::Anyref => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::Anyref.option()],
|
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations
This commit starts adding a test suite which checks in, to the
repository, test assertions for both the JS and wasm file outputs of a
Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be
exhaustive or large scale tests, but rather micro-tests to help observe
the changes in `wasm-bindgen`'s output over time.
The motivation for this commit is basically overhauling how all the GC
passes work in `wasm-bindgen` today. The reorganization is also included
in this commit as well.
Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes
of `walrus` in a bunch of places to ensure that less "garbage" was seen
by future passes. This not only was a source of slowdown but it also was
pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked
through.
The strategy taken in this commit is to have one precise location for a
GC pass, and everything goes through there. This is achieved by:
* All internal exports are removed immediately when generating the
nonstandard wasm interface types section. Internal exports,
intrinsics, and runtime support are all referenced by the various
instructions and/or sections that use them. This means that we now
have precise tracking of what an adapter uses.
* This in turn enables us to implement the `add_gc_roots` function for
`walrus` custom sections, which in turn allows walrus GC passes to do
what `unexport_unused_intrinsics` did before. That function is now no
longer necessary, but effectively works the same way. All intrinsics
are unexported at the beginning and then they're selectively
re-imported and re-exported through the JS glue generation pass as
necessary and defined by the bindings.
* Passes like the `anyref` pass are now much more precise about the
intrinsics that they work with. The `anyref` pass also deletes any
internal intrinsics found and also does some rewriting of the adapters
aftewards now to hook up calls to the heap count import to the heap
count intrinsic in the wasm module.
* Fix handling of __wbindgen_realloc
The final user of the `require_internal_export` function was
`__wbindgen_realloc`. This usage has now been removed by updating how we
handle usage of the `realloc` function.
The wasm interface types standard doesn't have a `realloc` function
slot, nor do I think it ever will. This means that as a polyfill for
wasm interface types we'll always have to support the lack of `realloc`.
For direct Rust to JS, however, we can still optionally handle
`realloc`. This is all handled with a few internal changes.
* Custom `StringToMemory` instructions now exist. These have an extra
`realloc` slot to store an intrinsic, if found.
* Our custom instructions are lowered to the standard instructions when
generating an interface types section.
* The `realloc` function, if present, is passed as an argument like the
malloc function when passing strings to wasm. If it's not present we
use a slower fallback, but if it's present we use the faster
implementation.
This should mean that there's little-to-no impact on existing users of
`wasm-bindgen`, but this should continue to still work for wasm
interface types polyfills and such. Additionally the GC passes now work
in that they don't delete `__wbindgen_realloc` which we later try to
reference.
* Add an empty test for the anyref pass
* Precisely track I32FromOptionAnyref's dependencies
This depends on the anyref table and a function to allocate an index if
the anyref pass is running, so be sure to track that in the instruction
itself for GC rooting.
* Trim extraneous exports from nop anyref module
Or if you're otherwise not using anyref slices, don't force some
intrinsics to exist.
* Remove globals from reference tests
Looks like these values adjust in slight but insignificant ways over
time
* Update the anyref xform tests
2019-12-04 12:01:39 -06:00
|
|
|
Instruction::I32FromOptionAnyref {
|
|
|
|
table_and_alloc: None,
|
|
|
|
},
|
2019-12-03 11:16:44 -06:00
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
2020-02-20 01:14:32 +10:00
|
|
|
Descriptor::NamedAnyref(name) => {
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::NamedAnyref(name.clone()).option()],
|
|
|
|
Instruction::I32FromOptionAnyref {
|
|
|
|
table_and_alloc: None,
|
|
|
|
},
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
2020-01-07 11:34:02 -06:00
|
|
|
Descriptor::I8 => self.in_option_sentinel(AdapterType::S8),
|
|
|
|
Descriptor::U8 => self.in_option_sentinel(AdapterType::U8),
|
|
|
|
Descriptor::I16 => self.in_option_sentinel(AdapterType::S16),
|
|
|
|
Descriptor::U16 => self.in_option_sentinel(AdapterType::U16),
|
2019-12-03 11:16:44 -06:00
|
|
|
Descriptor::I32 => self.in_option_native(ValType::I32),
|
|
|
|
Descriptor::U32 => self.in_option_native(ValType::I32),
|
|
|
|
Descriptor::F32 => self.in_option_native(ValType::F32),
|
|
|
|
Descriptor::F64 => self.in_option_native(ValType::F64),
|
|
|
|
Descriptor::I64 | Descriptor::U64 => {
|
2020-01-07 11:34:02 -06:00
|
|
|
let (signed, ty) = match arg {
|
|
|
|
Descriptor::I64 => (true, AdapterType::S64.option()),
|
|
|
|
_ => (false, AdapterType::U64.option()),
|
2019-12-03 11:16:44 -06:00
|
|
|
};
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[ty],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32SplitOption64 { signed },
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::I32, AdapterType::I32, AdapterType::I32],
|
2019-12-03 11:16:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Boolean => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::Bool.option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromOptionBool,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Char => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::String.option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromOptionChar,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::Enum { hole } => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::U32.option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromOptionEnum { hole: *hole },
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Descriptor::RustStruct(name) => {
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::Struct(name.clone()).option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromOptionRust {
|
|
|
|
class: name.to_string(),
|
|
|
|
},
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations
This commit starts adding a test suite which checks in, to the
repository, test assertions for both the JS and wasm file outputs of a
Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be
exhaustive or large scale tests, but rather micro-tests to help observe
the changes in `wasm-bindgen`'s output over time.
The motivation for this commit is basically overhauling how all the GC
passes work in `wasm-bindgen` today. The reorganization is also included
in this commit as well.
Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes
of `walrus` in a bunch of places to ensure that less "garbage" was seen
by future passes. This not only was a source of slowdown but it also was
pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked
through.
The strategy taken in this commit is to have one precise location for a
GC pass, and everything goes through there. This is achieved by:
* All internal exports are removed immediately when generating the
nonstandard wasm interface types section. Internal exports,
intrinsics, and runtime support are all referenced by the various
instructions and/or sections that use them. This means that we now
have precise tracking of what an adapter uses.
* This in turn enables us to implement the `add_gc_roots` function for
`walrus` custom sections, which in turn allows walrus GC passes to do
what `unexport_unused_intrinsics` did before. That function is now no
longer necessary, but effectively works the same way. All intrinsics
are unexported at the beginning and then they're selectively
re-imported and re-exported through the JS glue generation pass as
necessary and defined by the bindings.
* Passes like the `anyref` pass are now much more precise about the
intrinsics that they work with. The `anyref` pass also deletes any
internal intrinsics found and also does some rewriting of the adapters
aftewards now to hook up calls to the heap count import to the heap
count intrinsic in the wasm module.
* Fix handling of __wbindgen_realloc
The final user of the `require_internal_export` function was
`__wbindgen_realloc`. This usage has now been removed by updating how we
handle usage of the `realloc` function.
The wasm interface types standard doesn't have a `realloc` function
slot, nor do I think it ever will. This means that as a polyfill for
wasm interface types we'll always have to support the lack of `realloc`.
For direct Rust to JS, however, we can still optionally handle
`realloc`. This is all handled with a few internal changes.
* Custom `StringToMemory` instructions now exist. These have an extra
`realloc` slot to store an intrinsic, if found.
* Our custom instructions are lowered to the standard instructions when
generating an interface types section.
* The `realloc` function, if present, is passed as an argument like the
malloc function when passing strings to wasm. If it's not present we
use a slower fallback, but if it's present we use the faster
implementation.
This should mean that there's little-to-no impact on existing users of
`wasm-bindgen`, but this should continue to still work for wasm
interface types polyfills and such. Additionally the GC passes now work
in that they don't delete `__wbindgen_realloc` which we later try to
reference.
* Add an empty test for the anyref pass
* Precisely track I32FromOptionAnyref's dependencies
This depends on the anyref table and a function to allocate an index if
the anyref pass is running, so be sure to track that in the instruction
itself for GC rooting.
* Trim extraneous exports from nop anyref module
Or if you're otherwise not using anyref slices, don't force some
intrinsics to exist.
* Remove globals from reference tests
Looks like these values adjust in slight but insignificant ways over
time
* Update the anyref xform tests
2019-12-04 12:01:39 -06:00
|
|
|
Descriptor::String | Descriptor::CachedString => {
|
|
|
|
let malloc = self.cx.malloc()?;
|
|
|
|
let mem = self.cx.memory()?;
|
|
|
|
let realloc = self.cx.realloc();
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::String.option()],
|
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations
This commit starts adding a test suite which checks in, to the
repository, test assertions for both the JS and wasm file outputs of a
Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be
exhaustive or large scale tests, but rather micro-tests to help observe
the changes in `wasm-bindgen`'s output over time.
The motivation for this commit is basically overhauling how all the GC
passes work in `wasm-bindgen` today. The reorganization is also included
in this commit as well.
Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes
of `walrus` in a bunch of places to ensure that less "garbage" was seen
by future passes. This not only was a source of slowdown but it also was
pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked
through.
The strategy taken in this commit is to have one precise location for a
GC pass, and everything goes through there. This is achieved by:
* All internal exports are removed immediately when generating the
nonstandard wasm interface types section. Internal exports,
intrinsics, and runtime support are all referenced by the various
instructions and/or sections that use them. This means that we now
have precise tracking of what an adapter uses.
* This in turn enables us to implement the `add_gc_roots` function for
`walrus` custom sections, which in turn allows walrus GC passes to do
what `unexport_unused_intrinsics` did before. That function is now no
longer necessary, but effectively works the same way. All intrinsics
are unexported at the beginning and then they're selectively
re-imported and re-exported through the JS glue generation pass as
necessary and defined by the bindings.
* Passes like the `anyref` pass are now much more precise about the
intrinsics that they work with. The `anyref` pass also deletes any
internal intrinsics found and also does some rewriting of the adapters
aftewards now to hook up calls to the heap count import to the heap
count intrinsic in the wasm module.
* Fix handling of __wbindgen_realloc
The final user of the `require_internal_export` function was
`__wbindgen_realloc`. This usage has now been removed by updating how we
handle usage of the `realloc` function.
The wasm interface types standard doesn't have a `realloc` function
slot, nor do I think it ever will. This means that as a polyfill for
wasm interface types we'll always have to support the lack of `realloc`.
For direct Rust to JS, however, we can still optionally handle
`realloc`. This is all handled with a few internal changes.
* Custom `StringToMemory` instructions now exist. These have an extra
`realloc` slot to store an intrinsic, if found.
* Our custom instructions are lowered to the standard instructions when
generating an interface types section.
* The `realloc` function, if present, is passed as an argument like the
malloc function when passing strings to wasm. If it's not present we
use a slower fallback, but if it's present we use the faster
implementation.
This should mean that there's little-to-no impact on existing users of
`wasm-bindgen`, but this should continue to still work for wasm
interface types polyfills and such. Additionally the GC passes now work
in that they don't delete `__wbindgen_realloc` which we later try to
reference.
* Add an empty test for the anyref pass
* Precisely track I32FromOptionAnyref's dependencies
This depends on the anyref table and a function to allocate an index if
the anyref pass is running, so be sure to track that in the instruction
itself for GC rooting.
* Trim extraneous exports from nop anyref module
Or if you're otherwise not using anyref slices, don't force some
intrinsics to exist.
* Remove globals from reference tests
Looks like these values adjust in slight but insignificant ways over
time
* Update the anyref xform tests
2019-12-04 12:01:39 -06:00
|
|
|
Instruction::OptionString {
|
|
|
|
malloc,
|
|
|
|
mem,
|
|
|
|
realloc,
|
|
|
|
},
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
Add reference output tests for JS operations (#1894)
* Add reference output tests for JS operations
This commit starts adding a test suite which checks in, to the
repository, test assertions for both the JS and wasm file outputs of a
Rust crate compiled with `#[wasm_bindgen]`. These aren't intended to be
exhaustive or large scale tests, but rather micro-tests to help observe
the changes in `wasm-bindgen`'s output over time.
The motivation for this commit is basically overhauling how all the GC
passes work in `wasm-bindgen` today. The reorganization is also included
in this commit as well.
Previously `wasm-bindgen` would, in an ad-hoc fashion, run the GC passes
of `walrus` in a bunch of places to ensure that less "garbage" was seen
by future passes. This not only was a source of slowdown but it also was
pretty brittle since `wasm-bindgen` kept breaking if extra iteams leaked
through.
The strategy taken in this commit is to have one precise location for a
GC pass, and everything goes through there. This is achieved by:
* All internal exports are removed immediately when generating the
nonstandard wasm interface types section. Internal exports,
intrinsics, and runtime support are all referenced by the various
instructions and/or sections that use them. This means that we now
have precise tracking of what an adapter uses.
* This in turn enables us to implement the `add_gc_roots` function for
`walrus` custom sections, which in turn allows walrus GC passes to do
what `unexport_unused_intrinsics` did before. That function is now no
longer necessary, but effectively works the same way. All intrinsics
are unexported at the beginning and then they're selectively
re-imported and re-exported through the JS glue generation pass as
necessary and defined by the bindings.
* Passes like the `anyref` pass are now much more precise about the
intrinsics that they work with. The `anyref` pass also deletes any
internal intrinsics found and also does some rewriting of the adapters
aftewards now to hook up calls to the heap count import to the heap
count intrinsic in the wasm module.
* Fix handling of __wbindgen_realloc
The final user of the `require_internal_export` function was
`__wbindgen_realloc`. This usage has now been removed by updating how we
handle usage of the `realloc` function.
The wasm interface types standard doesn't have a `realloc` function
slot, nor do I think it ever will. This means that as a polyfill for
wasm interface types we'll always have to support the lack of `realloc`.
For direct Rust to JS, however, we can still optionally handle
`realloc`. This is all handled with a few internal changes.
* Custom `StringToMemory` instructions now exist. These have an extra
`realloc` slot to store an intrinsic, if found.
* Our custom instructions are lowered to the standard instructions when
generating an interface types section.
* The `realloc` function, if present, is passed as an argument like the
malloc function when passing strings to wasm. If it's not present we
use a slower fallback, but if it's present we use the faster
implementation.
This should mean that there's little-to-no impact on existing users of
`wasm-bindgen`, but this should continue to still work for wasm
interface types polyfills and such. Additionally the GC passes now work
in that they don't delete `__wbindgen_realloc` which we later try to
reference.
* Add an empty test for the anyref pass
* Precisely track I32FromOptionAnyref's dependencies
This depends on the anyref table and a function to allocate an index if
the anyref pass is running, so be sure to track that in the instruction
itself for GC rooting.
* Trim extraneous exports from nop anyref module
Or if you're otherwise not using anyref slices, don't force some
intrinsics to exist.
* Remove globals from reference tests
Looks like these values adjust in slight but insignificant ways over
time
* Update the anyref xform tests
2019-12-04 12:01:39 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Descriptor::Vector(_) => {
|
2019-12-03 11:16:44 -06:00
|
|
|
let kind = arg.vector_kind().ok_or_else(|| {
|
|
|
|
format_err!(
|
|
|
|
"unsupported optional slice type for calling Rust function from JS {:?}",
|
|
|
|
arg
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
let malloc = self.cx.malloc()?;
|
|
|
|
let mem = self.cx.memory()?;
|
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::Vector(kind).option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::OptionVector { kind, malloc, mem },
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
2019-12-03 11:16:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => bail!(
|
|
|
|
"unsupported optional argument type for calling Rust function from JS: {:?}",
|
|
|
|
arg
|
|
|
|
),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&mut self, ty: AdapterType) {
|
|
|
|
self.input.push(ty);
|
|
|
|
|
|
|
|
// If we're generating instructions in the return position then the
|
|
|
|
// arguments are already on the stack to consume, otherwise we need to
|
|
|
|
// fetch them from the parameters.
|
|
|
|
if !self.return_position {
|
|
|
|
let idx = self.input.len() as u32 - 1;
|
|
|
|
let std = wit_walrus::Instruction::ArgGet(idx);
|
|
|
|
self.instructions.push(InstructionData {
|
|
|
|
instr: Instruction::Standard(std),
|
|
|
|
stack_change: StackChange::Modified {
|
|
|
|
pushed: 1,
|
|
|
|
popped: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn instruction(
|
|
|
|
&mut self,
|
|
|
|
inputs: &[AdapterType],
|
|
|
|
instr: Instruction,
|
|
|
|
outputs: &[AdapterType],
|
|
|
|
) {
|
|
|
|
// If we're generating instructions in the return position then the
|
|
|
|
// arguments are already on the stack to consume, otherwise we need to
|
|
|
|
// fetch them from the parameters.
|
|
|
|
if !self.return_position {
|
|
|
|
for input in inputs {
|
2020-01-07 11:34:02 -06:00
|
|
|
self.get(input.clone());
|
2019-12-03 11:16:44 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.input.extend_from_slice(inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.instructions.push(InstructionData {
|
|
|
|
instr,
|
|
|
|
stack_change: StackChange::Modified {
|
|
|
|
popped: inputs.len(),
|
|
|
|
pushed: outputs.len(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
self.output.extend_from_slice(outputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn number(&mut self, input: wit_walrus::ValType, output: walrus::ValType) {
|
|
|
|
let std = wit_walrus::Instruction::IntToWasm {
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
trap: false,
|
|
|
|
};
|
|
|
|
self.instruction(
|
|
|
|
&[AdapterType::from_wit(input)],
|
|
|
|
Instruction::Standard(std),
|
|
|
|
&[AdapterType::from_wasm(output).unwrap()],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn number64(&mut self, signed: bool) {
|
|
|
|
self.instruction(
|
|
|
|
&[if signed {
|
|
|
|
AdapterType::S64
|
|
|
|
} else {
|
|
|
|
AdapterType::U64
|
|
|
|
}],
|
|
|
|
Instruction::I32Split64 { signed },
|
|
|
|
&[AdapterType::I32, AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_option_native(&mut self, wasm: ValType) {
|
2020-01-07 11:34:02 -06:00
|
|
|
let ty = AdapterType::from_wasm(wasm).unwrap();
|
2019-12-03 11:16:44 -06:00
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[ty.clone().option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::FromOptionNative { ty: wasm },
|
2020-01-07 11:34:02 -06:00
|
|
|
&[AdapterType::I32, ty],
|
2019-12-03 11:16:44 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-07 11:34:02 -06:00
|
|
|
fn in_option_sentinel(&mut self, ty: AdapterType) {
|
2019-12-03 11:16:44 -06:00
|
|
|
self.instruction(
|
2020-01-07 11:34:02 -06:00
|
|
|
&[ty.option()],
|
2019-12-03 11:16:44 -06:00
|
|
|
Instruction::I32FromOptionU32Sentinel,
|
|
|
|
&[AdapterType::I32],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|