Skip to content

Commit

Permalink
Add import D function usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
chances committed Dec 27, 2020
1 parent 1c21418 commit 092c30a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,58 @@ const string wat_sum_module =
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_);
auto sumFunc = Function.from(instance.exports[0]);
assert(instance.valid, "Could not instantiate module!");
assert(engine.valid && store.valid && module_.valid && instance.valid && sumFunc.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

```d
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](https://opensource.org/licenses/MIT)
Expand Down
46 changes: 43 additions & 3 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ <h2 id="Usage">Usage</h2>
}
</pre>
<p>See the official <a href="https://github.com/wasmerio/wasmer/tree/master/lib/c-api#readme">Wasmer Runtime C API</a> documentation.</p>
<h3>Run a WebAssembly Module</h3>
<h3 id="run">Run a WebAssembly Module</h3>
<p>
Sum function in a WebAssembly <a href="https://webassembly.github.io/spec/core/text/index.html">text format</a> module:
</p>
Expand All @@ -101,14 +101,54 @@ <h3>Run a WebAssembly Module</h3>
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_);
auto sumFunc = Function.from(instance.exports[0]);
assert(instance.valid, "Could not instantiate module!");

assert(engine.valid && store.valid && module_.valid && instance.valid && sumFunc.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);
</pre>
<h3 id="import-d-function">Import a D Function into a WebAssembly Module</h3>
<pre class="c">
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_);
</pre>
<!-- <h2>Acknowledgements</h2> -->
<h2>Modules</h2>

0 comments on commit 092c30a

Please sign in to comment.