Binaryen: Call functions with params || Creat WebAssembly with extern "c"

Created on 25 Dec 2016  路  2Comments  路  Source: WebAssembly/binaryen

My intend is to execute the following functions:

    int counter = 100;

    int count() {
        return counter += 1;
    }

    int add(int a, int b){
        return a + b;
    }

I followed this tutorial
and create a docker container for the webassembly toolchain which allows me to build a *.wasm from *.c with the following command:
docker run --rm --name generate-wasm-default -v ${PWD}:/src -i -t chrisber/llvm-webassembly:latest /bin/build.sh hello_world.c
Basically it does the following:

clang -emit-llvm --target=wasm32 -S hello_world.c
llc hello_world.ll -march=wasm32
s2wasm hello_world.s > hello_world.wast
wast2wasm -o hello_world.wasm hello_world.wast

Then calling within index.js:

const dependencies = {
    "global": {},
    "env": {}
};
const instance = new WebAssembly.Instance(module, dependencies);
const wasm = instance.exports;

Calling the function count:
console.log("count function result is : " + wasm.count()); works just fine, but calling:
console.log("add function result is : " + wasm.add(1,1)); leads to the following error:

Uncaught (in promise) RuntimeError: memory access out of bounds
    at  (<WASM>[1]+15)
    at http://localhost:3000/main.bundle.js:39:60
(anonymous) @ wasm-9d2fd5a6-1:9
(anonymous) @ index.ts:20

My higher goal would be to call functions linked from a shared library like:.

clang -emit-llvm --target=wasm32 -S hello_world.c -L. -lxyz and calling it with

extern "C" {
    //....
}

but the error Uncaught (in promise) RuntimeError: memory access out of bounds prevents me from
experimenting with extern "c".

The project is located here.

I'm stuck with this issue and your help is highly appreciated.

Most helpful comment

That tutorial does things in a nonstandard and not recommended way. It happens to work on the things in the tutorial, but other stuff likely won't.

In general the recommended path is to use emscripten normally, and add the flag -s WASM=1. That is the recommended path. It emits a wasm plus a JS file that provides all the runtime support it needs.

If you want only wasm, without JS, then there is still a better way to do it than that tutorial. See the standalone wasm option, it lets you build a wasm that you can write the loading code for yourself. More similar to that tutorial, but it uses the recommended tools. It is not fully stable yet, though, feedback is welcome.

All 2 comments

That tutorial does things in a nonstandard and not recommended way. It happens to work on the things in the tutorial, but other stuff likely won't.

In general the recommended path is to use emscripten normally, and add the flag -s WASM=1. That is the recommended path. It emits a wasm plus a JS file that provides all the runtime support it needs.

If you want only wasm, without JS, then there is still a better way to do it than that tutorial. See the standalone wasm option, it lets you build a wasm that you can write the loading code for yourself. More similar to that tutorial, but it uses the recommended tools. It is not fully stable yet, though, feedback is welcome.

Hi!
We tracked this issue and after reading it we created a new tutorial. https://tutorials.technology/tutorials/54-Webassembly-tutorial-using-emscripten-hello-world.html

Please let us know if you find mistakes or if there is a better way of working with webassembly.
we want to help people!

thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aheejin picture aheejin  路  3Comments

juj picture juj  路  11Comments

tsangint picture tsangint  路  5Comments

kripken picture kripken  路  11Comments

brakmic picture brakmic  路  9Comments