tree: c549e2c78192b4b72d288f6335470045678bc7a6 [path history] [tgz]
  1. bin/
  2. example/
  3. lib/
  4. test/
  5. tool/
  6. .gitignore
  7. analysis_options.yaml
  8. AUTHORS
  9. LICENSE
  10. pubspec.yaml
  11. README.md
pkg/wasm/README.md

Provides utilities for loading and running WASM modules

Built on top of the Wasmer runtime.

Setup

Run dart run wasm:setup to build the Wasmer runtime.

Basic Usage

As a simple example, we'll try to call the following C function from Dart using package:wasm. For a more detailed example that uses WASI, check out the example directory.

extern "C" int square(int n) { return n * n; }

We can compile this C++ code to WASM using a recent version of clang:

clang --target=wasm32 -nostdlib -Wl,--export-all -Wl,--no-entry -o square.wasm square.cc

Then we can load and run it like this:

import 'dart:io';
import 'package:wasm/wasm.dart';

void main() {
  final data = File('square.wasm').readAsBytesSync();
  final mod = WasmModule(data);
  print(mod.describe());
  final inst = mod.instantiate().build();
  final square = inst.lookupFunction('square');
  print(square(12));
}

This should print:

export memory: memory
export function: int32 square(int32)

144