1

I have a demo fiddle online at https://webassembly.studio/?f=0r9gxzb9rdq where I'm using an array buffer that gets shared between JS and WebAssembly.

As suggested in this answer How to access WebAssembly linear memory from C/C++, I'm declaring it as a global array that I'm reading and writing to, along with a helper function to return the starting address:

const SIZE = 6 * 1000 * 1000;
int array[SIZE]

WASM_EXPORT
int* getStartByteOffset() {
  return &array[0];
}

Then, in JS-land, once I load the wasm file and get the module instance:

const SIZE = 6 * canvas.width * canvas.height;
const heap = instance.exports.memory.buffer;
const offset = instance.exports.getStartByteOffset()
const arrayView = new Int32Array(heap, offset, SIZE)

This works, and now JS and WebAssembly can access this array. But it's not the way I want to do it. I don't know the required array size beforehand (the canvas could turn out to be any size), so I'm idiotically preallocating one large enough to accommodate 1000x1000 pixels (where each pixel needs six 32-bit wide ints).

What I want to do is calculate in JS the memory required, and pass it in as a WebAssembly.Memory once I know how many 64k pages I need:

  fetch('../out/main.wasm').then(response => response.arrayBuffer())
    .then((bytes) => {
      WebAssembly.instantiate(bytes, {
        env: {
          memoryBase: 0,
          memory: new WebAssembly.Memory({
            initial: 1 + ((6 * 4 * canvas.width * canvas.height) >> 16)
          })
        }
      }).then((wasm) => {...});
    });

What I can't figure out is the precise way to access the WebAssembly.Memory buffer using C. The Internet is crammed full of code samples on how the JS side of the API works, but all the code samples I can ever find on the WebAssembly side are written in WAT.

jtiscione
  • 1,131
  • 8
  • 13
  • The actual question is: why would one want to do that?? – too honest for this site Aug 27 '18 at 19:43
  • For the reason I just gave- I don't know the required array size until runtime. – jtiscione Aug 27 '18 at 19:52
  • And why do you need webassembly for this and don't use native C? But, hey, some shoot through the spine, the knee into the head. – too honest for this site Aug 27 '18 at 19:54
  • Hey, if you can explain how I can get people's browsers to run C code natively, I'm all ears! – jtiscione Aug 27 '18 at 20:10
  • Erm, you are aware your question implies you want to run C? Nevermind, I'll let you think a bit about what your actual problem is. As it seems now, it's an XY. Better make it an XX (or YY). – too honest for this site Aug 27 '18 at 20:15
  • It doesn't imply I "want to run C". C is a language and doesn't "run", it gets compiled. If I want to run WebAssembly code, the easiest way to get it is to write it in C and compile to WASM. There are more compilation targets for C than you seem to think. – jtiscione Aug 27 '18 at 20:32
  • You did make me realize my mistake- I asked a question about WebAssembly and tagged it with "C". – jtiscione Aug 27 '18 at 22:13
  • A WebAssembly.Memory instance is full address space what a C program can access. Isn't `malloc()` what you need? Perhaps more memory is required than `1 + ((6 * 4 * canvas.width * canvas.height) >> 16)`. – zakki Aug 28 '18 at 06:38
  • Yes, I'm looking for something like malloc, but in the native API. `Module.malloc` is only available in an Enscripten-generated JS glue code file that I'm trying to avoid. (The `>>16` is to convert from bytes to the number of 64k pages.) – jtiscione Aug 28 '18 at 14:07

0 Answers0