Skip to content
pubscout

wasm_ffi

2.4.0, released 3 months ago by vm75.duckdns.org

Drop-in replacement for dart:ffi for WebAssembly on the web platform.

WASM-ready
$ flutter pub add wasm_ffi
Source repository

What it does

Provides a dart:ffi-like API for WebAssembly modules on the web supporting both standalone Wasm and Emscripten JavaScript glue. It handles memory mapping, pointers, and function lookups for Dart web applications compiled with dart2js or dart2wasm.

AI-written summary of version 2.4.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to run native C/C++ Wasm modules within a Flutter or Dart web application.
  • You want a cross-platform codebase that uses dart:ffi on native and wasm_ffi on the web.

Skip it when

  • You are building exclusively for native platforms where standard dart:ffi is available.
  • You require complex unsupported advanced types or shared memory across multiple libraries without custom setup.

Strengths

  • Enables reuse of existing C/C++ codebases via WebAssembly on the web.
  • Mirrors the familiar dart:ffi API pattern closely.

Weaknesses

  • Separately loaded Wasm modules do not share memory by default.
  • Strict return type constraints and special rules apply when looking up and invoking functions.

Setup

Kind
FFI (native library)
Effort
Medium
Code generation
No
Direct dependencies
5

Requires building Wasm modules using Emscripten with specific flags (like EXPORTED_RUNTIME_METHODS=HEAPU8 or STANDALONE_WASM=1) and setting up proxy FFI imports.

import 'package:wasm_ffi/ffi.dart';

typedef AddNative = Int32 Function(Int32, Int32);
typedef AddDart = int Function(int, int);

Future<void> main() async {
  final library = await DynamicLibrary.open('assets/example.wasm');
  final add = library.lookupFunction<AddNative, AddDart>('add');
  print(add(2, 3));
  await library.close();
}

Alternatives

Packages that do the same job and could replace wasm_ffi.

Works with

Packages commonly used together with wasm_ffi: ones it builds on, ones built on it, and its own family.