feat(node): support node classes

This commit is contained in:
Ashley Williams 2018-03-29 12:12:54 +02:00
parent a0bfb8103b
commit 256b34736e
2 changed files with 41 additions and 1 deletions

View File

@ -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;

View File

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