[WASM] Access WebAssembly Memory Directly from JavaScript
While JavaScript has a garbage-collected heap, WebAssembly has a linear memory space. Nevertheless using a JavaScript ArrayBuffer, we can read and write to WebAssembly’s memory space.
lib.rs:

#[macro_use] extern crate cfg_if; extern crate wasm_bindgen; use wasm_bindgen::prelude::*; cfg_if! { // When the `console_error_panic_hook` feature is enabled, we can call the // `set_panic_hook` function to get better error messages if we ever panic. if #[cfg(feature = "console_error_panic_hook")] { extern crate console_error_panic_hook; use console_error_panic_hook::set_once as set_panic_hook; } } cfg_if! { // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. if #[cfg(feature = "wee_alloc")] { extern crate wee_alloc; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; } } // Definitions of the functionality available in JS, which wasm-bindgen will // generate shims for today (and eventually these should be near-0 cost!) // // These definitions need to be hand-written today but the current vision is // that we'll use WebIDL to generate this `extern` block into a crate which you // can link and import. There's a tracking issue for this at // https://github.com/rustwasm/wasm-bindgen/issues/42 // // In the meantime these are written out by hand and correspond to the names and // signatures documented on MDN, for example #[wasm_bindgen] extern "C" { type HTMLDocument; static document: HTMLDocument; #[wasm_bindgen(method)] fn createElement(this: &HTMLDocument, tagName: &str) -> Element; #[wasm_bindgen(method, getter)] fn body(this: &HTMLDocument) -> Element; type Element; #[wasm_bindgen(method, setter = innerHTML)] fn set_inner_html(this: &Element, html: &str); #[wasm_bindgen(method, js_name = appendChild)] fn append_child(this: &Element, other: Element); #[wasm_bindgen(js_namespace = console)] fn log(msg: &str); } macro_rules! log { ($($t:tt)*) => (log(&format!($($t)*))) }
#[wasm_bindgen] pub struct Color { red: u8, green: u8, blue: u8, } #[wasm_bindgen] pub struct Image { pixels: Vec<Color>, } #[wasm_bindgen] impl Image { pub fn new() -> Image { let color1 = Color { red: 255, green: 0, blue: 0, }; let color2 = Color { red: 60, green: 70, blue: 90, }; let pixels = vec![color1, color2]; Image { pixels } } pub fn pixels_ptr(&self) -> *const Color { self.pixels.as_ptr() } }
We create a Image object, which has two functions, new() and pixels_ptr() which can be called by javascript. new() is a constructor function, pixels_ptr() is a instant method.
app.js:
import { memory } from "../crate/pkg/rust_webpack_bg"; import { Image } from "../crate/pkg/rust_webpack"; const image = Image.new(); const pixelsPointer = image.pixels_ptr(); const pixels = new Uint8Array(memory.buffer, pixelsPointer, 6); console.log(pixels); function numToHex(value) { const hex = value.toString(16); return hex.length === 1 ? `0${hex}` : hex; } function drawPixel(x, y, color) { const ctx = canvas.getContext("2d"); ctx.fillStyle = `#${numToHex(color[0])}${numToHex(color[1])}${numToHex( color[2] )}`; ctx.fillRect(x, y, 100, 100); } const canvas = document.createElement("canvas"); document.body.appendChild(canvas); drawPixel(0, 0, pixels.slice(0, 3)); drawPixel(100, 0, pixels.slice(3, 6));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-11-07 [Javascript] Validate Data with the Every() Method
2016-11-07 [Immutable.js] Updating nested values with ImmutableJS