Merge pull request #70 from Healthire/struct-into-js

Support passing custom types by value into JS imports
This commit is contained in:
Alex Crichton 2018-03-15 19:29:08 -05:00 committed by GitHub
commit 7e1b31ab2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

View File

@ -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");
}

View File

@ -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()