Merge pull request #1278 from alexcrichton/examples

Update idioms of a few examples
This commit is contained in:
Alex Crichton 2019-02-19 16:14:32 -06:00 committed by GitHub
commit 8f695782fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 27 deletions

View File

@ -3,19 +3,6 @@
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<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 -->
<script src='./pkg/no_modules.js'></script>

View File

@ -216,7 +216,6 @@
<canvas id='canvas'></canvas>
<script>
delete WebAssembly.instantiateStreaming;
document.getElementById('render').disabled = true;
document.getElementById('concurrency').disabled = true;
</script>

View File

@ -18,20 +18,19 @@ const WASM: &[u8] = include_bytes!("add.wasm");
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
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
// `WebAssembly.Memory` buffer then if we allocate more pages for ourself
// (aka do a memory allocation in Rust) it'll cause the buffer to change.
// That means we can't actually do any memory allocations after we do this
// until we pass it back to JS.
let my_memory = Uint8Array::new(&my_memory.buffer()).subarray(
WASM.as_ptr() as u32,
WASM.as_ptr() as u32 + WASM.len() as u32,
);
let a = WebAssembly::Module::new(my_memory.as_ref())?;
// Note that `Uint8Array::view` this is somewhat dangerous (hence the
// `unsafe`!). This is creating a raw view into our module's
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
// causing the `Uint8Array` to be invalid.
//
// As a result, after `Uint8Array::view` we have to be very careful not to
// do any memory allocations before it's next used.
let a = unsafe {
let array = Uint8Array::view(WASM);
WebAssembly::Module::new(array.as_ref())?
};
let b = WebAssembly::Instance::new(&a, &Object::new())?;
let c = b.exports();