Add support for Option<&T> in imported argument lists

Closes #619
This commit is contained in:
Alex Crichton
2018-08-02 22:28:13 -07:00
parent fe51cf9857
commit 88db12669f
2 changed files with 15 additions and 5 deletions

View File

@ -125,6 +125,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
self.cx.expose_take_object(); self.cx.expose_take_object();
self.js_arguments.push(format!("takeObject({})", abi)); self.js_arguments.push(format!("takeObject({})", abi));
return Ok(()) return Ok(())
} else if arg.is_ref_anyref() {
self.cx.expose_get_object();
self.js_arguments.push(format!("getObject({})", abi));
return Ok(())
} }
if optional { if optional {
@ -253,10 +257,6 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
ref d if d.is_number() => abi, ref d if d.is_number() => abi,
Descriptor::Boolean => format!("{} !== 0", abi), Descriptor::Boolean => format!("{} !== 0", abi),
Descriptor::Char => format!("String.fromCodePoint({})", abi), Descriptor::Char => format!("String.fromCodePoint({})", abi),
ref d if d.is_ref_anyref() => {
self.cx.expose_get_object();
format!("getObject({})", abi)
}
_ => bail!( _ => bail!(
"unimplemented argument type in imported function: {:?}", "unimplemented argument type in imported function: {:?}",
arg arg

View File

@ -13,6 +13,11 @@ extern {
fn return_null_byval() -> Option<MyType>; fn return_null_byval() -> Option<MyType>;
fn return_some_byval() -> Option<MyType>; fn return_some_byval() -> Option<MyType>;
fn test_option_values(); fn test_option_values();
#[wasm_bindgen(js_name = take_none_byval)]
fn take_none_byref(t: Option<&MyType>);
#[wasm_bindgen(js_name = take_some_byval)]
fn take_some_byref(t: Option<&MyType>);
} }
#[wasm_bindgen_test] #[wasm_bindgen_test]
@ -24,7 +29,6 @@ fn import_by_value() {
assert!(return_some_byval().is_some()); assert!(return_some_byval().is_some());
} }
#[wasm_bindgen_test] #[wasm_bindgen_test]
fn export_by_value() { fn export_by_value() {
test_option_values(); test_option_values();
@ -49,3 +53,9 @@ pub fn rust_return_none_byval() -> Option<MyType> {
pub fn rust_return_some_byval() -> Option<MyType> { pub fn rust_return_some_byval() -> Option<MyType> {
Some(MyType::new()) Some(MyType::new())
} }
#[wasm_bindgen_test]
fn import_by_ref() {
take_none_byref(None);
take_some_byref(Some(&MyType::new()));
}