diff --git a/examples/no_modules/index.html b/examples/no_modules/index.html index 979a8621..4844908e 100644 --- a/examples/no_modules/index.html +++ b/examples/no_modules/index.html @@ -3,19 +3,6 @@ - - diff --git a/examples/raytrace-parallel/index.html b/examples/raytrace-parallel/index.html index 22258875..bc9cae70 100644 --- a/examples/raytrace-parallel/index.html +++ b/examples/raytrace-parallel/index.html @@ -216,7 +216,6 @@ diff --git a/examples/wasm-in-wasm/src/lib.rs b/examples/wasm-in-wasm/src/lib.rs index 1fa56668..4a4ab553 100644 --- a/examples/wasm-in-wasm/src/lib.rs +++ b/examples/wasm-in-wasm/src/lib.rs @@ -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::() - .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();