Skip to content

Commit

Permalink
pass the data from js to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
zhentian-wan committed Oct 20, 2018
1 parent fa525c6 commit ae646a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
23 changes: 21 additions & 2 deletions index.html
Expand Up @@ -2,12 +2,31 @@
<html>
<head>
<script>
WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"))

// pass the data from Js to Rust
const appendNumberToBody = (number) => {
const text = document.createTextNode(number);
document.body.appendChild(text);
}

const importObject = {
env: {
appendNumberToBody: appendNumberToBody,
alert: alert
}
};

WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"), importObject)
.then(wasmModule => {
wasmModule.instance.exports.run();
});

/*WebAssembly.instantiateStreaming(fetch("utils.gc.wasm"))
.then(wasmModule => {
const result = wasmModule.instance.exports.add_one(3);
const text = document.createTextNode(result);
document.body.appendChild(text);
});
});*/
</script>
<head>
<body></body>
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
@@ -1,4 +1,17 @@
#[no_mangle]
pub extern fn add_one(x: u32) -> u32 {
x + 1
}

extern {
fn appendNumberToBody(x: u32);
fn alert(x: u32);
}

#[no_mangle]
pub extern fn run() {
unsafe {
appendNumberToBody(42);
alert(33)
}
}
Binary file modified utils.gc.wasm
Binary file not shown.

0 comments on commit ae646a6

Please sign in to comment.