From b5f288830c42494fef2e1b555ad417c602bf890f Mon Sep 17 00:00:00 2001 From: Rich Lane Date: Fri, 17 Feb 2023 14:47:13 -0800 Subject: [PATCH] Optimize getting byteLength in MemoryView::new --- lib/api/src/js/externals/memory_view.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/api/src/js/externals/memory_view.rs b/lib/api/src/js/externals/memory_view.rs index 347547e8664..78b29247eb6 100644 --- a/lib/api/src/js/externals/memory_view.rs +++ b/lib/api/src/js/externals/memory_view.rs @@ -6,6 +6,7 @@ use std::mem::MaybeUninit; use std::slice; #[cfg(feature = "tracing")] use tracing::warn; +use wasm_bindgen::JsCast; use wasmer_types::{Bytes, Pages}; @@ -33,10 +34,11 @@ impl<'a> MemoryView<'a> { pub(crate) fn new_raw(memory: &js_sys::WebAssembly::Memory) -> Self { let buffer = memory.buffer(); - let size = js_sys::Reflect::get(&buffer, &"byteLength".into()) - .unwrap() - .as_f64() - .unwrap() as u64; + // This also works for SharedArrayBuffer. + let size = buffer + .unchecked_ref::() + .byte_length() + .into(); let view = js_sys::Uint8Array::new(&buffer);