Monday, July 10, 2023

polywasm: WASM interpreter in JavaScript, polyfill for WebAssembly

evanw/polywasm @GitHub

"This is a polyfill for WebAssembly. It implements enough of the WebAssembly API to be able to run a .wasm file in a JavaScript environment that lacks a WebAssembly implementation. This is done by parsing the .wasm file and translating each WebAssembly function to a JavaScript function. This is not nearly as fast as running WebAssembly natively, but it's better than it not running at all.

Why does this exist?

WebAssembly support is already widespread so you don't normally need a polyfill to use it. However, certain modern JavaScript environments have WebAssembly disabled. For example, Apple's Lockdown Mode (an opt-in security enhancement) disables WebAssembly in Safari. You can use this polyfill to make a WebAssembly-based app work in Safari in Lockdown Mode anyway. It will be extremely slow because Lockdown Mode also disables JavaScript optimizations, but sometimes performance isn't critical.

Another reason to use this might be to play around with WebAssembly execution. For example, this polyfill makes it pretty trivial to instrument each function call to add caller/callee tracing or to make a copy of memory before/after a function call, which could be useful for debugging."