Getting Started
🚀 Installation
You can install wasp-lib using your favorite package manager.
bash
npm install wasp-libbash
yarn add wasp-libbash
pnpm add wasp-libbash
bun add wasp-lib📖 Quick Start Guide
Step 1: Import the Library
Import the necessary pointer classes and the WASMModule type for type-safe integration.
typescript
import {
StringPointer,
ArrayPointer,
NumberPointer,
CharPointer,
BoolPointer,
} from "wasp-lib";
import type { WASMModule } from "wasp-lib";Step 2: Initialize Your WASM Module
Load your Emscripten-generated WASM module. The resulting module object should be cast to the WASMModule type.
typescript
// Assuming you have a WASM module generated by Emscripten
import Module from "./your-wasm-module.js";
async function initWasm(): Promise<WASMModule> {
const wasm: WASMModule = await Module();
return wasm;
}Step 3: Use Pointer Classes
Now you can use the wasp-lib pointer classes to safely interact with your WebAssembly module's memory. wasp-lib provides classes for different data types:
StringPointer- C-style null-terminated stringsNumberPointer- Single numeric values (int, float, double)ArrayPointer- Numeric arrays with type safetyCharPointer- Single character valuesBoolPointer- Boolean values (stored as 0/1)
typescript
async function example() {
const wasm = await initWasm();
// String operations
const greeting = StringPointer.from(wasm, 50, "Hello");
wasm._process_string(greeting.ptr);
console.log(greeting.readAndFree()); // e.g., "Hello World!" (modified by WASM)
// Array operations
const numbers = [1, 2, 3, 4, 5];
const arrayPtr = ArrayPointer.from(wasm, "i32", numbers.length, numbers);
const sum = wasm._sum_array(arrayPtr.ptr, numbers.length);
arrayPtr.free();
console.log(sum); // 15
// Number operations
const valuePtr = NumberPointer.from(wasm, "double", 3.14159);
wasm._square_value(valuePtr.ptr);
console.log(valuePtr.readAndFree()); // 9.869...
}