npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ubique-innovation/wasm-share-memory

v0.1.2

Published

A utility package that allows sharing memory between multiple WASM modules.

Readme

WASM Share Memory

Relocatable WebAssembly Modules with Shared Memory Layouts

Combine multiple WebAssembly (WASM) modules into a single linear memory space — enabling efficient shared-memory communication without linking at compile time.

✨ Features

  • 🧠 Relocates multiple standalone WASM modules into a shared memory space
  • 🧩 Preserves individual module logic and memory layout
  • 🧾 Modifies globals and memory segments for compatibility

📦 Installation

npm install @ubique-innovation/wasm-share-memory

🛠️ Usage

The WASM modules must be compiled with the following RUSTFLAGS and compiler flags:

# import-memory: allows the WASM module to import memory from the host environment
# relocation-model=pic: enables position-independent code for relocatable modules
export RUSTFLAGS="-C link-arg=--import-memory -Crelocation-model=pic"

# Currently, the nightly toolchain is required, because the std, alloc, and core libraries also need to be compiled with `relocation-model=pic`, otherwise they will generate absolute addresses.
cargo +nightly build -Z build-std="core,std,alloc,panic_abort" --target wasm32-unknown-unknown --release
# Install the package
pnpm install @ubique-innovation/wasm-share-memory
import * as fs from 'fs';
import { combine } from '@ubique-innovation/wasm-share-memory';

// Load the WASM modules as Uint8Arrays
const libcommon = fs.readFileSync('./output/common.wasm');
const liba = fs.readFileSync('./output/a.wasm');
const libb = fs.readFileSync('./output/b.wasm');

// Layout: common | liba | libb | shared heap
const { modules, neededPages } = await combine([libcommon, liba, libb]);

// Create shared memory
const memory = new WebAssembly.Memory({
    initial: neededPages,
});

// Instantiate modules with the shared memory
const { instance: icommon } = await WebAssembly.instantiate(modules[0], {
    env: { memory },
});
const { instance: ia } = await WebAssembly.instantiate(modules[1], {
    env: { memory },
});
const { instance: ib } = await WebAssembly.instantiate(modules[2], {
    env: { memory },
});

// Example usage of shared memory:
//
// ```rust
// // common.rs
// struct Object { value: i64 }
//
// pub fn create_object(value: i64) -> *mut Object;
//
// // liba.rs
// pub fn get_value(object: &Object) -> i64;
//
// // libb.rs
// pub fn double(object: *mut Object);
// ```

// Instantiate an 'Object' using the common library.
const object = icommon.exports.create_object(1337n);

// Use the 'get_value' function from liba to read the value.
const value = ia.exports.get_value(object);
console.log(value); // 1337n

// Use the 'double' function from libb to modify the value.
ib.exports.double(object);

// Read the modified value back from liba.
// This will return the doubled value.
const doubled = ia.exports.get_value(object);
console.log(doubled); // 2674n

🧬 How It Works

  • Uses a Rust+WASM backend (via walrus) to:
    • Inspect and rewrite WASM modules.
    • Patch __wasm_init_memory to initialize the shifted memory.
    • Patch all global variables with values between __stack_pointer and __heap_base.
    • Relocate globals and data segments to prevent overlap.
    • Patch __heap_base, __stack_pointer, and __memory_base.
  • JavaScript/TypeScript wrapper loads and prepares modules in the browser or Node.js.

For more internal details, check the Rust source code.

🧪 Functions

combine(modules: Uint8Array[], additionalStack?: number): Promise<{ modules, neededPages }>

Relocates modules sequentially and returns modified binaries and required memory pages for the shared memory layout.

getHeapBase(module: Uint8Array): Promise<number>

Extracts the __heap_base global.

relocate(module: Uint8Array, offset: number, newHeapBase: number): Promise<Uint8Array>

Relocates a single module to a new memory offset and adjusts __heap_base.

📂 Project Structure

  • rust-lib/: WASM backend implementing the relocation logic. Packaged using wasm-pack.
  • src/index.ts: TS wrapper logic for loading and interacting with the backend
  • example/: Example project demonstrating usage in a NodeJS environment.
  • example/www/: Example web project demonstrating usage in a browser environment.

🚀 Building

Package

# Install dependencies
pnpm install

# Build the rust library (WASM) and the package
pnpm build

# Build only the Rust library
pnpm run build:wasm

# Build only the package (depends on the WASM library)
pnpm run build:package

Example

cd example

# Install dependencies
pnpm install

# Build the example rust libraries
./build.sh

# Run the example
pnpm start

Example (Web)

cd example/www

# Install dependencies
pnpm install

# Build the example rust libraries and copy them
./build.sh

# Start the web server
python3 -m http.server 8000

📄 License

MIT