tuanha1305 / wasm

Utilities for loading and running WASM modules from Dart code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.builder().build();
  final square = inst.lookupFunction('square');
  print(square(12));
}

This should print:

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

144

About

Utilities for loading and running WASM modules from Dart code

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Dart 84.7%Language:Python 8.2%Language:Perl 6.3%Language:C++ 0.7%Language:Rust 0.0%