Giter Site home page Giter Site logo

wasmer-d's Introduction

wasmer-d

DUB Package wasmer-d CI codecov

D bindings to wasmer, a standalone WebAssembly runtime for running WebAssembly outside of the browser.

Also includes an idiomatic D wrapper of the Wasmer Runtime C API.

Usage

"dependencies": {
    "wasmer": "0.2.0"
}

See the official Wasmer Runtime C API documentation.

Run a WebAssembly Module

Sum function in a WebAssembly text format module:

const string wat_sum_module =
  "(module\n" ~
  "  (type $sum_t (func (param i32 i32) (result i32)))\n" ~
  "  (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)\n" ~
  "    local.get $x\n" ~
  "    local.get $y\n" ~
  "    i32.add)\n" ~
  "  (export \"sum\" (func $sum_f)))";

auto engine = new Engine();
auto store = new Store(engine);
auto module_ = Module.from(store, wat_sum_module);
assert(engine.valid && store.valid && module_.valid, "Could not load module!");

auto instance = new Instance(store, module_);
assert(instance.valid, "Could not instantiate module!");

assert(instance.exports[0].name == "sum");
auto sumFunc = Function.from(instance.exports[0]);
assert(sumFunc.valid, "Could not load exported 'sum' function!");

Value[] results;
assert(sumFunc.call([new Value(3), new Value(4)], results), "Error calling the `sum` function!");
assert(results.length == 1 && results[0].value.of.i32 == 7);

Import a D Function into a WebAssembly Module

const string wat_callback_module =
  "(module" ~
  "  (func $print (import \"\" \"print\") (param i32) (result i32))" ~
  "  (func (export \"run\") (param $x i32) (param $y i32) (result i32)" ~
  "    (call $print (i32.add (local.get $x) (local.get $y)))" ~
  "  )" ~
  ")";

auto engine = new Engine();
auto store = new Store(engine);
auto module_ = Module.from(store, wat_callback_module);
assert(module_.valid, "Error compiling module!");

auto print = (Module module_, int value) => {
  return value;
}();
auto imports = [new Function(store, module_, print.toDelegate).asExtern];
auto instance = module_.instantiate(imports);
assert(instance.valid, "Could not instantiate module!");

auto runFunc = Function.from(instance.exports[0]);
assert(instance.exports[0].name == "run" && runFunc.valid, "Failed to get the `run` function!");

auto three = new Value(3);
auto four = new Value(4);
Value[] results;
assert(runFunc.call([three, four], results), "Error calling the `run` function!");
assert(results.length == 1 && results[0].value.of.i32 == 7);

destroy(three);
destroy(four);
destroy(instance);
destroy(module_);

License

MIT Licence

Copyright © 2020-2021 Chance Snow. All rights reserved.

wasmer-d's People

Contributors

chances avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.