Using functions in WebAssembly, we can pass individual numeric values. We're restricted to integers and floats, either 32 bits or 64 bits. The standard WASM filler example has a function that returns a number. When we build and run this, we can log that number. We're passing numbers through the interfaces.
One option would be to create a function, getChar, that returns individual characters at a single offset. We're still returning a number representing the character, but we can call it many times to get the whole string.
We could then use some code to convert that into the correct string character, and we could read out the string letter-by-letter. Thankfully, there's a better way to do this. Instead of a getChar function, I'm going to create a char pointer-returning function called getStringOffset.
What this function will do is it'll return the memory address of the first character of the string. When we build this, we can see in the code below, getStringOffset is returning a single number, which is a 32-bit integer. It's a constant integer, 16.
This is the address of our string in the WebAssembly memory. If we look further up in this output, we can see that there's a data section. The data section is used to create pre-allocated memory in our WebAssembly modules.
This data section is allocating at address number 16, the contents "hello world." Then our memory is also being exported over here. We've got this single piece of addressable linear memory that contains the data we want.
We want to create a typed array on top of this WebAssembly memory that represents our string. Each character of the string is eight bits. I'm going to use the Uint8 array typed array on top of the WebAssembly memory, the memory.buffer property, and then specify the offset within the WebAssembly memory that our string starts.