diff --git a/crates/wasm-bindgen-cli-support/src/js.rs b/crates/wasm-bindgen-cli-support/src/js.rs index eb16c61f9..bb68974d9 100644 --- a/crates/wasm-bindgen-cli-support/src/js.rs +++ b/crates/wasm-bindgen-cli-support/src/js.rs @@ -248,7 +248,12 @@ impl<'a> Context<'a> { let classes = mem::replace(&mut self.exported_classes, Default::default()); for (class, exports) in classes { let mut dst = String::new(); - dst.push_str(&format!("export class {} {{", class)); + let global_export = if self.config.nodejs { + format!("module.exports.{} = class {} {{\n", class, class) + } else { + format!("export class {} {{", class) + }; + dst.push_str(&global_export); let mut ts_dst = dst.clone(); ts_dst.push_str(" public ptr: number; diff --git a/tests/node.rs b/tests/node.rs index a4fddeb19..d0118e6b6 100644 --- a/tests/node.rs +++ b/tests/node.rs @@ -20,6 +20,25 @@ fn works() { pub fn run() { hit(); } + + #[wasm_bindgen] + pub struct Foo { + contents: u32, + } + + #[wasm_bindgen] + impl Foo { + pub fn new() -> Foo { + Foo::with_contents(0) + } + pub fn with_contents(a: u32) -> Foo { + Foo { contents: a } + } + pub fn add(&mut self, amt: u32) -> u32 { + self.contents += amt; + self.contents + } + } "#) .file("test.js", r#" const assert = require('assert'); @@ -34,7 +53,23 @@ fn works() { module.exports.test = function() { run(); assert.strictEqual(called, true); + var Foo = run.Foo; + + var r = Foo.new(); + assert.strictEqual(r.contents, 0); + assert.strictEqual(r.add(0), 0); + assert.strictEqual(r.add(1), 1); + assert.strictEqual(r.add(2), 2); + r.free(); + + var r2 = Foo.with_contents(10); + assert.strictEqual(r2.contents, 10); + assert.strictEqual(r2.add(0), 0); + assert.strictEqual(r2.add(1), 1); + assert.strictEqual(r2.add(2), 2); + r2.free(); }; + "#) .test(); }