digitalmars.D.learn - WASM - Passing JS objects to D
What is the recommended way to pass JS objects to D when doing WASM? My current solution is to maintain an ID <-> object mapping in JS, something like: const mapJsToHandle = new WeakMap(); const mapHandleToJs = {}; var id = 0; const toHandle = (x) => { if (!mapJsToHandle.has(x)) { id++; mapJsToHandle.set(x, id); mapHandleToJs[id] = new WeakRef(x); return id; } else { return mapJsToHandle.get(x); } }; const fromHandle = (x) => { return mapHandleToJs[x].deref(); }; which I can use like: const drawJS = (imghandle, x, y) => { document.ctx.drawImage(fromHandle(imghandle), x, y); } var img = new Image(); drawImage(toHandle(img)) ============ D code ============ extern(C) void drawImage(int imghandle) { draw(imghandle, 100, 100); } void drawJS(int imghandle, double x, double y); This works. But I wonder if there's some easier/better way? If I understand correctly you can only pass primitive objects and arrays across the boundaries.
Jul 25 2023
Think about the readers when you paste code https://www.markdownguide.org/extended-syntax/#syntax-highlighting
Jul 25 2023