Passing High Level Data Types with as-bind

Overview

Using buffers and pointers, is a great way to get started with WebAssembly, and drill in its concepts while being productive. But once we start wanting to use higher level data structures efficiently and easily, is where things will get a little more complicated. Thankfully, the AssemblyScript community built as-bind, which is a convenient abstraction over AssemblyScript's loader. As mentioned before, as-bind abstracts away linear memory, and allows using higher-level data structures between AssemblyScript and JavaScript.

Let's kick things off! To show off how we can use as-bind, let's see how we can use strings in WebAssembly and share them with JavaScript:

What you will notice that is interesting, in other examples we've had to ensure all of our exported functions returned a supported WebAssembly number type. Now that we will be using as-bind, we can export a function that returns a string!

Now, let's compile our assemblyscript. as-bind has an entry file that must be included when compiling your AssemblyScript. Therefore, we would run:

Another nice thing to notice, as-bind will handle our WebAssembly instantion for us! This is because during as-bind's instantiation step, it is wrapping our export function calls with their own as-bind function calls. The original exports can still be accessed at: asBindInstance.unboundExports.

Lastly, lets create an index.html similar to one described in the hello-world, and load our index.js Javascript file in our index.html. Then, you should get something similar to the demo (Source Code) below!

Demo

as-bind supports more than just strings, and has support for many different types! I'd highly reccomend reading the as-bind README, and in particular, the section of the supported types by as-bind.