react-native-android-webassembly
v0.0.2
Published
Android module for executing WebAssembly through JavaScriptSandbox.
Readme
react-native-android-webassembly
Android module for running WebAssembly through androidx.javascriptengine.
This package exports a standard WebAssembly ponyfill for React Native. It is
designed as a drop-in replacement for browser-style WebAssembly.* usage, and
it keeps .wasm binaries out of the JS bundle by loading them from Metro
assets at runtime.
Public API
WebAssemblyModuleInstanceMemoryTableGlobalTagExceptionCompileErrorLinkErrorRuntimeErrormakeImportClosure(...)polyfillGlobalWebAssembly()clearWasmHandles()
Asset loading
Treat .wasm files as Metro assets, then resolve the asset URI and pass the
bytes into WebAssembly.instantiate() or WebAssembly.compile().
import {WebAssembly} from 'react-native-android-webassembly';
const asset = require('./assets/my-module.wasm');
const uri =
typeof asset === 'string'
? asset
: asset?.uri ?? require('react-native/Libraries/Image/resolveAssetSource').default(asset).uri;
const response = await fetch(uri);
const bytes = await response.arrayBuffer();
const {instance} = await WebAssembly.instantiate(bytes);If you want the ponyfill to be available on globalThis, call
polyfillGlobalWebAssembly() once during app startup.
Example app
The repository includes an emulator-ready app under example/ that exercises
the standard WebAssembly API, including asset-backed module loading and the WPT
compatibility harness.
Run the Metro server:
npm run example:startInstall and launch on Android:
npm run example:androidFor the fastest local development loop, link the package into the example once after installing dependencies:
npm run example:linkTests
Unit tests currently live in test/assetUri.test.js and run with:
npm test