mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-07-12 02:51:32 +00:00
Add support for optional slice types (#507)
* Shard the `convert.rs` module into sub-modules Hopefully this'll make the organization a little nicer over time! * Start adding support for optional types This commit starts adding support for optional types to wasm-bindgen as arguments/return values to functions. The strategy here is to add two new traits, `OptionIntoWasmAbi` and `OptionFromWasmAbi`. These two traits are used as a blanket impl to implement `IntoWasmAbi` and `FromWasmAbi` for `Option<T>`. Some consequences of this design: * It should be possible to ensure `Option<SomeForeignType>` implements to/from wasm traits. This is because the option-based traits can be implemented for foreign types. * A specialized implementation is possible for all types, so there's no need for `Option<T>` to introduce unnecessary overhead. * Two new traits is a bit unforutnate but I can't currently think of an alternative design that works for the above two constraints, although it doesn't mean one doesn't exist! * The error messages for "can't use this type here" is actually halfway decent because it says these new traits need to be implemented, which provides a good place to document and talk about what's going on here! * Nested references like `Option<&T>` can't implement `FromWasmAbi`. This means that you can't define a function in Rust which takes `Option<&str>`. It may be possible to do this one day but it'll likely require more trait trickery than I'm capable of right now. * Add support for optional slices This commit adds support for optional slice types, things like strings and arrays. The null representation of these has a pointer value of 0, which should never happen in normal Rust. Otherwise the various plumbing is done throughout the tooling to enable these types in all locations. * Fix `takeObject` on global sentinels These don't have a reference count as they're always expected to work, so avoid actually dropping a reference on them. * Remove some no longer needed bindings * Add support for optional anyref types This commit adds support for optional imported class types. Each type imported with `#[wasm_bindgen]` automatically implements the relevant traits and now supports `Option<Foo>` in various argument/return positions. * Fix building without the `std` feature * Actually fix the build... * Add support for optional types to WebIDL Closes #502
This commit is contained in:
crates
backend
src
cli-support
web-sys
tests
all
webidl
src
guide/src
src
tests/all
@ -82,25 +82,36 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
fn argument(&mut self, arg: &Descriptor) -> Result<(), Error> {
|
||||
let abi = self.shim_argument();
|
||||
|
||||
let (arg, optional) = match arg {
|
||||
Descriptor::Option(t) => (&**t, true),
|
||||
_ => (arg, false),
|
||||
};
|
||||
|
||||
if let Some(ty) = arg.vector_kind() {
|
||||
let abi2 = self.shim_argument();
|
||||
let f = self.cx.expose_get_vector_from_wasm(ty);
|
||||
self.prelude(&format!(
|
||||
"let v{0} = {func}({0}, {1});",
|
||||
"let v{0} = {prefix}{func}({0}, {1});",
|
||||
abi,
|
||||
abi2,
|
||||
func = f
|
||||
func = f,
|
||||
prefix = if optional { format!("{} == 0 ? undefined : ", abi) } else { String::new() },
|
||||
));
|
||||
|
||||
if !arg.is_by_ref() {
|
||||
self.prelude(&format!(
|
||||
"\
|
||||
v{0} = v{0}.slice();\n\
|
||||
wasm.__wbindgen_free({0}, {1} * {size});\
|
||||
{start}
|
||||
v{0} = v{0}.slice();
|
||||
wasm.__wbindgen_free({0}, {1} * {size});
|
||||
{end}\
|
||||
",
|
||||
abi,
|
||||
abi2,
|
||||
size = ty.size()
|
||||
size = ty.size(),
|
||||
start = if optional { format!("if ({} !== 0) {{", abi) } else { String::new() },
|
||||
end = if optional { "}" } else { "" },
|
||||
|
||||
));
|
||||
self.cx.require_internal_export("__wbindgen_free")?;
|
||||
}
|
||||
@ -108,6 +119,18 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// No need to special case `optional` here because `takeObject` will
|
||||
// naturally work.
|
||||
if arg.is_anyref() {
|
||||
self.cx.expose_take_object();
|
||||
self.js_arguments.push(format!("takeObject({})", abi));
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
if optional {
|
||||
bail!("unsupported optional argument {:?}", arg);
|
||||
}
|
||||
|
||||
if let Some(signed) = arg.get_64bit() {
|
||||
let f = if signed {
|
||||
self.cx.expose_int64_cvt_shim()
|
||||
@ -233,10 +256,6 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
ref d if d.is_number() => abi,
|
||||
Descriptor::Boolean => format!("{} !== 0", abi),
|
||||
Descriptor::Char => format!("String.fromCodePoint({})", abi),
|
||||
Descriptor::Anyref => {
|
||||
self.cx.expose_take_object();
|
||||
format!("takeObject({})", abi)
|
||||
}
|
||||
ref d if d.is_ref_anyref() => {
|
||||
self.cx.expose_get_object();
|
||||
format!("getObject({})", abi)
|
||||
@ -258,6 +277,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let (ty, optional) = match ty {
|
||||
Descriptor::Option(t) => (&**t, true),
|
||||
_ => (ty, false),
|
||||
};
|
||||
if ty.is_by_ref() {
|
||||
bail!("cannot return a reference from JS to Rust")
|
||||
}
|
||||
@ -265,17 +288,43 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
let f = self.cx.pass_to_wasm_function(ty)?;
|
||||
self.cx.expose_uint32_memory();
|
||||
self.shim_arguments.insert(0, "ret".to_string());
|
||||
let mut prelude = String::new();
|
||||
let expr = if optional {
|
||||
prelude.push_str("const val = JS;");
|
||||
self.cx.expose_is_like_none();
|
||||
format!("isLikeNone(val) ? [0, 0] : {}(val)", f)
|
||||
} else {
|
||||
format!("{}(JS)", f)
|
||||
};
|
||||
self.ret_expr = format!(
|
||||
"\
|
||||
const [retptr, retlen] = {}(JS);\n\
|
||||
{}
|
||||
const [retptr, retlen] = {};
|
||||
const mem = getUint32Memory();
|
||||
mem[ret / 4] = retptr;
|
||||
mem[ret / 4 + 1] = retlen;
|
||||
",
|
||||
f
|
||||
prelude,
|
||||
expr
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
if ty.is_anyref() {
|
||||
self.cx.expose_add_heap_object();
|
||||
if optional {
|
||||
self.cx.expose_is_like_none();
|
||||
self.ret_expr = "
|
||||
const val = JS;
|
||||
return isLikeNone(val) ? 0 : addHeapObject(val);
|
||||
".to_string();
|
||||
} else {
|
||||
self.ret_expr = "return addHeapObject(JS);".to_string()
|
||||
}
|
||||
return Ok(())
|
||||
}
|
||||
if optional {
|
||||
bail!("unsupported optional return type {:?}", ty);
|
||||
}
|
||||
if ty.is_number() {
|
||||
self.ret_expr = "return JS;".to_string();
|
||||
return Ok(());
|
||||
@ -324,10 +373,6 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
self.ret_expr = match *ty {
|
||||
Descriptor::Boolean => "return JS ? 1 : 0;".to_string(),
|
||||
Descriptor::Char => "return JS.codePointAt(0);".to_string(),
|
||||
Descriptor::Anyref => {
|
||||
self.cx.expose_add_heap_object();
|
||||
"return addHeapObject(JS);".to_string()
|
||||
}
|
||||
_ => bail!("unimplemented return from JS to Rust: {:?}", ty),
|
||||
};
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user