Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

wasm

provides utilities for loading and running wasm modules

built on top of the wasmer runtime.

setup

start by installing the tools needed to build the wasmer runtime:

basic usage

as a simple example, we’ll try to call a simple c function from dart using
package:wasm. for a more detailed example that uses wasi, check out the
example directory.

  1. first create new dart app folder: dart create wasmtest
  2. add a dependency to package wasm in pubspec.yaml and run dart pub get
  3. next run dart run wasm:setup to build the wasmer runtime. this will take a few minutes.
  4. then add a new file square.cc with the following contents:
    extern "c" int square(int n) { return n * n; }
    
  5. we can compile this c++ code to wasm using a recent version of the
    clang compiler:

    clang --target=wasm32 -nostdlib "-wl,--export-all" "-wl,--no-entry" -o square.wasm square.cc
    
  6. replace the contents of your dart program (bin/wasmtest.dart) with:
    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));
    }
    
  7. run the dart program: dart run. this should print:
    export memory: memory
    export function: int32 square(int32)
    
    144
    

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.