Skip to content

Commit 8f69578

Browse files
authored
Merge pull request #1278 from alexcrichton/examples
Update idioms of a few examples
2 parents e66de7b + b8f080d commit 8f69578

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

examples/no_modules/index.html

-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
44
</head>
55
<body>
6-
<script>
7-
// The `--no-modules`-generated JS from `wasm-bindgen` attempts to use
8-
// `WebAssembly.instantiateStreaming` to instantiate the wasm module,
9-
// but this doesn't work with `file://` urls. This example is frequently
10-
// viewed by simply opening `index.html` in a browser (with a `file://`
11-
// url), so it would fail if we were to call this function!
12-
//
13-
// Work around this for now by deleting the function to ensure that the
14-
// `no_modules.js` script doesn't have access to it. You won't need this
15-
// hack when deploying over HTTP.
16-
delete WebAssembly.instantiateStreaming;
17-
</script>
18-
196
<!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
207
<script src='./pkg/no_modules.js'></script>
218

examples/raytrace-parallel/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@
216216
<canvas id='canvas'></canvas>
217217

218218
<script>
219-
delete WebAssembly.instantiateStreaming;
220219
document.getElementById('render').disabled = true;
221220
document.getElementById('concurrency').disabled = true;
222221
</script>

examples/wasm-in-wasm/src/lib.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ const WASM: &[u8] = include_bytes!("add.wasm");
1818
#[wasm_bindgen(start)]
1919
pub fn run() -> Result<(), JsValue> {
2020
console_log!("instantiating a new wasm module directly");
21-
let my_memory = wasm_bindgen::memory()
22-
.dyn_into::<WebAssembly::Memory>()
23-
.unwrap();
24-
25-
// Note that this is somewhat dangerous, once we look at our
26-
// `WebAssembly.Memory` buffer then if we allocate more pages for ourself
27-
// (aka do a memory allocation in Rust) it'll cause the buffer to change.
28-
// That means we can't actually do any memory allocations after we do this
29-
// until we pass it back to JS.
30-
let my_memory = Uint8Array::new(&my_memory.buffer()).subarray(
31-
WASM.as_ptr() as u32,
32-
WASM.as_ptr() as u32 + WASM.len() as u32,
33-
);
34-
let a = WebAssembly::Module::new(my_memory.as_ref())?;
21+
22+
// Note that `Uint8Array::view` this is somewhat dangerous (hence the
23+
// `unsafe`!). This is creating a raw view into our module's
24+
// `WebAssembly.Memory` buffer, but if we allocate more pages for ourself
25+
// (aka do a memory allocation in Rust) it'll cause the buffer to change,
26+
// causing the `Uint8Array` to be invalid.
27+
//
28+
// As a result, after `Uint8Array::view` we have to be very careful not to
29+
// do any memory allocations before it's next used.
30+
let a = unsafe {
31+
let array = Uint8Array::view(WASM);
32+
WebAssembly::Module::new(array.as_ref())?
33+
};
3534
let b = WebAssembly::Instance::new(&a, &Object::new())?;
3635
let c = b.exports();
3736

0 commit comments

Comments
 (0)