mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-01 18:01:06 +00:00
Fix the constructor explanation in the guide (#372)
This commit is contained in:
parent
9339a55f3e
commit
66096804ce
@ -8,16 +8,16 @@ function assertEq(a, b) {
|
|||||||
|
|
||||||
assertEq(concat('a', 'b'), 'ab');
|
assertEq(concat('a', 'b'), 'ab');
|
||||||
|
|
||||||
// Note the `new Foo()` syntax cannot be used, static function
|
// Note that to use `new Foo()` the constructor function must be annotated
|
||||||
// constructors must be used instead. Additionally objects allocated
|
// with `#[wasm_bindgen(constructor)]`, otherwise only `Foo.new()` can be used.
|
||||||
// corresponding to Rust structs will need to be deallocated on the
|
// Additionally objects allocated corresponding to Rust structs will need to
|
||||||
// Rust side of things with an explicit call to `free`.
|
// be deallocated on the Rust side of things with an explicit call to `free`.
|
||||||
let foo = Foo.new();
|
let foo = new Foo();
|
||||||
assertEq(foo.add(10), 10);
|
assertEq(foo.add(10), 10);
|
||||||
foo.free();
|
foo.free();
|
||||||
|
|
||||||
// Pass objects to one another
|
// Pass objects to one another
|
||||||
let foo1 = Foo.new();
|
let foo1 = new Foo();
|
||||||
let bar = Bar.from_str("22", { opaque: 'object' });
|
let bar = Bar.from_str("22", { opaque: 'object' });
|
||||||
foo1.add_other(bar);
|
foo1.add_other(bar);
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ pub struct Foo {
|
|||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Foo {
|
impl Foo {
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
pub fn new() -> Foo {
|
pub fn new() -> Foo {
|
||||||
Foo { contents: 0 }
|
Foo { contents: 0 }
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ pub struct Foo {
|
|||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Foo {
|
impl Foo {
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
pub fn new() -> Foo {
|
pub fn new() -> Foo {
|
||||||
Foo { contents: 0 }
|
Foo { contents: 0 }
|
||||||
}
|
}
|
||||||
@ -110,16 +111,16 @@ function assertEq(a, b) {
|
|||||||
function main() {
|
function main() {
|
||||||
assertEq(concat('a', 'b'), 'ab');
|
assertEq(concat('a', 'b'), 'ab');
|
||||||
|
|
||||||
// Note the `new Foo()` syntax cannot be used, static function
|
// Note that to use `new Foo()` the constructor function must be annotated
|
||||||
// constructors must be used instead. Additionally objects allocated
|
// with `#[wasm_bindgen(constructor)]`, otherwise only `Foo.new()` can be used.
|
||||||
// corresponding to Rust structs will need to be deallocated on the
|
// Additionally objects allocated corresponding to Rust structs will need to
|
||||||
// Rust side of things with an explicit call to `free`.
|
// be deallocated on the Rust side of things with an explicit call to `free`.
|
||||||
let foo = Foo.new();
|
let foo = new Foo();
|
||||||
assertEq(foo.add(10), 10);
|
assertEq(foo.add(10), 10);
|
||||||
foo.free();
|
foo.free();
|
||||||
|
|
||||||
// Pass objects to one another
|
// Pass objects to one another
|
||||||
let foo1 = Foo.new();
|
let foo1 = new Foo();
|
||||||
let bar = Bar.from_str("22", { opaque: 'object' });
|
let bar = Bar.from_str("22", { opaque: 'object' });
|
||||||
foo1.add_other(bar);
|
foo1.add_other(bar);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user