Skip to content

Commit bf44fcb

Browse files
authored
Merge pull request #626 from alexcrichton/optional-arg
Add support for `Option<&T>` in imported argument lists
2 parents d47fc61 + 88db126 commit bf44fcb

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

crates/cli-support/src/js/rust2js.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
125125
self.cx.expose_take_object();
126126
self.js_arguments.push(format!("takeObject({})", abi));
127127
return Ok(())
128+
} else if arg.is_ref_anyref() {
129+
self.cx.expose_get_object();
130+
self.js_arguments.push(format!("getObject({})", abi));
131+
return Ok(())
128132
}
129133

130134
if optional {
@@ -253,10 +257,6 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
253257
ref d if d.is_number() => abi,
254258
Descriptor::Boolean => format!("{} !== 0", abi),
255259
Descriptor::Char => format!("String.fromCodePoint({})", abi),
256-
ref d if d.is_ref_anyref() => {
257-
self.cx.expose_get_object();
258-
format!("getObject({})", abi)
259-
}
260260
_ => bail!(
261261
"unimplemented argument type in imported function: {:?}",
262262
arg

tests/wasm/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ extern crate wasm_bindgen_test;
55
extern crate wasm_bindgen;
66

77
pub mod api;
8+
pub mod option;

tests/wasm/option.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const assert = require('assert');
2+
const wasm = require('wasm-bindgen-test.js');
3+
4+
class MyType {}
5+
6+
exports.MyType = MyType;
7+
8+
exports.take_none_byval = function(x) {
9+
assert.strictEqual(x, undefined);
10+
};
11+
exports.take_some_byval = function(x) {
12+
assert.ok(x !== null && x !== undefined);
13+
assert.ok(x instanceof MyType);
14+
};
15+
exports.return_undef_byval = function() { return undefined; };
16+
exports.return_null_byval = function() { return null; };
17+
exports.return_some_byval = function(x) {
18+
return new MyType();
19+
};
20+
21+
exports.test_option_values = function() {
22+
wasm.rust_take_none_byval(null);
23+
wasm.rust_take_none_byval(undefined);
24+
wasm.rust_take_some_byval(new MyType());
25+
assert.strictEqual(wasm.rust_return_none_byval(), undefined);
26+
const x = wasm.rust_return_some_byval();
27+
assert.ok(x !== null && x !== undefined);
28+
assert.ok(x instanceof MyType);
29+
};

tests/wasm/option.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use wasm_bindgen_test::*;
2+
use wasm_bindgen::prelude::*;
3+
4+
#[wasm_bindgen(module = "tests/wasm/option.js", version = "*")]
5+
extern {
6+
pub type MyType;
7+
#[wasm_bindgen(constructor)]
8+
fn new() -> MyType;
9+
10+
fn take_none_byval(t: Option<MyType>);
11+
fn take_some_byval(t: Option<MyType>);
12+
fn return_undef_byval() -> Option<MyType>;
13+
fn return_null_byval() -> Option<MyType>;
14+
fn return_some_byval() -> Option<MyType>;
15+
fn test_option_values();
16+
17+
#[wasm_bindgen(js_name = take_none_byval)]
18+
fn take_none_byref(t: Option<&MyType>);
19+
#[wasm_bindgen(js_name = take_some_byval)]
20+
fn take_some_byref(t: Option<&MyType>);
21+
}
22+
23+
#[wasm_bindgen_test]
24+
fn import_by_value() {
25+
take_none_byval(None);
26+
take_some_byval(Some(MyType::new()));
27+
assert!(return_null_byval().is_none());
28+
assert!(return_undef_byval().is_none());
29+
assert!(return_some_byval().is_some());
30+
}
31+
32+
#[wasm_bindgen_test]
33+
fn export_by_value() {
34+
test_option_values();
35+
}
36+
37+
#[wasm_bindgen]
38+
pub fn rust_take_none_byval(t: Option<MyType>) {
39+
assert!(t.is_none());
40+
}
41+
42+
#[wasm_bindgen]
43+
pub fn rust_take_some_byval(t: Option<MyType>) {
44+
assert!(t.is_some());
45+
}
46+
47+
#[wasm_bindgen]
48+
pub fn rust_return_none_byval() -> Option<MyType> {
49+
None
50+
}
51+
52+
#[wasm_bindgen]
53+
pub fn rust_return_some_byval() -> Option<MyType> {
54+
Some(MyType::new())
55+
}
56+
57+
#[wasm_bindgen_test]
58+
fn import_by_ref() {
59+
take_none_byref(None);
60+
take_some_byref(Some(&MyType::new()));
61+
}

0 commit comments

Comments
 (0)