WebAssembly Compute Sample

Description

How to run: In Fusion, open Scripts and Add-Ins, choose WasmComputeSample under sample scripts, and click Run (or Edit to inspect all files). The full runnable project ships with Fusion.

What this page is: This help topic explains the WebAssembly loading pattern for TypeScript scripts. It does not include every file required to run the sample on its own.

Not shown on this page: The prebuilt wasm-bindgen bundle (wasm_compute.bundle.ts / .js), the sidecar binary fusion_wasm_compute_bg.wasm, and maintainer rebuild steps. Those files are in the installed sample folder; see README.md there for layout and how to rebuild the bundle.

Demonstrates compute-only WebAssembly: a minimal Rust function compiled with wasm-bindgen, with Fusion API usage kept in TypeScript. Fusion loads .ts and .js from your script folder but not raw .wasm as ES modules; load the binary with adsk.readBufferSync and wasm-bindgen sync init in the bundle.

Code Samples

Do not copy only the TypeScript below into a new script unless you also copy all companion files from the installed sample folder (wasm_compute.bundle.ts, fusion_wasm_compute_bg.wasm, and related files).

Recommended: Run the installed WasmComputeSample from Scripts and Add-Ins first. Use this page to understand how the entry script calls into the prebuilt bundle.

Installed sample layout

Loading pattern (in the generated bundle at module load):

initSync(adsk.readBufferSync("fusion_wasm_compute_bg.wasm"));

Limitations: Cannot call adsk from inside WASM; no ES module import './foo.wasm'; no runtime fetch / import.meta.url WASM loading in the bundle; use adsk.readBufferSync on a sidecar .wasm file, not string-oriented read/decode for WASM bytes.

/**
 * WASM compute sample (FUS-271999).
 *
 * Loads a pre-bundled wasm-bindgen module (sidecar .wasm via readBufferSync)
 * and calls an exported Rust function. Fusion API usage stays in TypeScript.
 *
 * Rebuild wasm_compute.bundle.ts: node build.mjs (see README.md).
 */

import { adsk } from "@adsk/fusion";
import { add } from "./wasm_compute.bundle.js";

export async function run(): Promise<void> {
    if (typeof WebAssembly !== "object") {
        throw new Error("WebAssembly unavailable");
    }

    const result = add(2, 3);
    if (result !== 5) {
        throw new Error(`add(2, 3) expected 5, got ${result}`);
    }

    adsk.log(`WasmComputeSample: add(2, 3) = ${result}`);
}

run().catch((error: unknown) => {
    const message = error instanceof Error ? error.stack ?? error.message : String(error);
    adsk.log(`WasmComputeSample failed: ${message}`);
    throw error;
});