Giter Site home page Giter Site logo

wasabia / wasm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dart-archive/wasm

0.0 0.0 0.0 1022 KB

Utilities for loading and running WASM modules from Dart code

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

C++ 0.65% Dart 84.05% Rust 0.02% Python 8.22% Perl 6.95% Raku 0.12%

wasm's Introduction

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
    

wasm's People

Contributors

kevmoo avatar liamappelbe avatar mit-mit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.