diff --git a/crates/wasm-bindgen-cli-support/src/js.rs b/crates/wasm-bindgen-cli-support/src/js.rs index e8c1bd225..a772f82e1 100644 --- a/crates/wasm-bindgen-cli-support/src/js.rs +++ b/crates/wasm-bindgen-cli-support/src/js.rs @@ -1402,6 +1402,17 @@ impl<'a, 'b> SubContext<'a, 'b> { invoc_args.push(format!("getObject(arg{})", i)); abi_args.push(format!("arg{}", i)); } + custom if (custom as u32) & shared::TYPE_CUSTOM_REF_FLAG == 0 => { + let s = self.cx.custom_type_name(custom).to_string(); + abi_args.push(format!("ptr{}", i)); + let assign = if self.cx.config.debug { + format!("let arg{0} = new {class}(ptr{0}, token);", i, class = s) + } else { + format!("let arg{0} = new {class}(ptr{0});", i, class = s) + }; + extra.push_str(&assign); + invoc_args.push(format!("arg{}", i)); + } _ => { panic!("unsupported type in import"); } diff --git a/tests/classes.rs b/tests/classes.rs index 6c60cca8d..1c7ebd962 100644 --- a/tests/classes.rs +++ b/tests/classes.rs @@ -225,6 +225,53 @@ fn pass_one_to_another() { .test(); } +#[test] +fn pass_into_js() { + test_support::project() + .file("src/lib.rs", r#" + #![feature(proc_macro)] + + extern crate wasm_bindgen; + + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + pub struct Foo(i32); + + #[wasm_bindgen] + impl Foo { + pub fn inner(&self) -> i32 { + self.0 + } + } + + #[wasm_bindgen(module = "./test")] + extern { + fn take_foo(foo: Foo); + } + + #[wasm_bindgen] + pub fn run() { + take_foo(Foo(13)); + } + "#) + .file("test.ts", r#" + import { run, Foo } from "./out"; + import * as assert from "assert"; + + export function take_foo(foo: Foo) { + assert.strictEqual(foo.inner(), 13); + foo.free(); + assert.throws(() => foo.free(), /null pointer passed to rust/); + } + + export function test() { + run(); + } + "#) + .test(); +} + #[test] fn issue_27() { test_support::project()