mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +00:00
Merge pull request #1278 from alexcrichton/examples
Update idioms of a few examples
This commit is contained in:
commit
8f695782fb
@ -3,19 +3,6 @@
|
|||||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script>
|
|
||||||
// The `--no-modules`-generated JS from `wasm-bindgen` attempts to use
|
|
||||||
// `WebAssembly.instantiateStreaming` to instantiate the wasm module,
|
|
||||||
// but this doesn't work with `file://` urls. This example is frequently
|
|
||||||
// viewed by simply opening `index.html` in a browser (with a `file://`
|
|
||||||
// url), so it would fail if we were to call this function!
|
|
||||||
//
|
|
||||||
// Work around this for now by deleting the function to ensure that the
|
|
||||||
// `no_modules.js` script doesn't have access to it. You won't need this
|
|
||||||
// hack when deploying over HTTP.
|
|
||||||
delete WebAssembly.instantiateStreaming;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
|
<!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
|
||||||
<script src='./pkg/no_modules.js'></script>
|
<script src='./pkg/no_modules.js'></script>
|
||||||
|
|
||||||
|
@ -216,7 +216,6 @@
|
|||||||
<canvas id='canvas'></canvas>
|
<canvas id='canvas'></canvas>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
delete WebAssembly.instantiateStreaming;
|
|
||||||
document.getElementById('render').disabled = true;
|
document.getElementById('render').disabled = true;
|
||||||
document.getElementById('concurrency').disabled = true;
|
document.getElementById('concurrency').disabled = true;
|
||||||
</script>
|
</script>
|
||||||
|
@ -18,20 +18,19 @@ const WASM: &[u8] = include_bytes!("add.wasm");
|
|||||||
#[wasm_bindgen(start)]
|
#[wasm_bindgen(start)]
|
||||||
pub fn run() -> Result<(), JsValue> {
|
pub fn run() -> Result<(), JsValue> {
|
||||||
console_log!("instantiating a new wasm module directly");
|
console_log!("instantiating a new wasm module directly");
|
||||||
let my_memory = wasm_bindgen::memory()
|
|
||||||
.dyn_into::<WebAssembly::Memory>()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Note that this is somewhat dangerous, once we look at our
|
// Note that `Uint8Array::view` this is somewhat dangerous (hence the
|
||||||
// `WebAssembly.Memory` buffer then if we allocate more pages for ourself
|
// `unsafe`!). This is creating a raw view into our module's
|
||||||
// (aka do a memory allocation in Rust) it'll cause the buffer to change.
|
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
|
||||||
// That means we can't actually do any memory allocations after we do this
|
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
|
||||||
// until we pass it back to JS.
|
// causing the `Uint8Array` to be invalid.
|
||||||
let my_memory = Uint8Array::new(&my_memory.buffer()).subarray(
|
//
|
||||||
WASM.as_ptr() as u32,
|
// As a result, after `Uint8Array::view` we have to be very careful not to
|
||||||
WASM.as_ptr() as u32 + WASM.len() as u32,
|
// do any memory allocations before it's next used.
|
||||||
);
|
let a = unsafe {
|
||||||
let a = WebAssembly::Module::new(my_memory.as_ref())?;
|
let array = Uint8Array::view(WASM);
|
||||||
|
WebAssembly::Module::new(array.as_ref())?
|
||||||
|
};
|
||||||
let b = WebAssembly::Instance::new(&a, &Object::new())?;
|
let b = WebAssembly::Instance::new(&a, &Object::new())?;
|
||||||
let c = b.exports();
|
let c = b.exports();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user