vendredi 27 juillet 2018

WebAssembly: Correct way to get a string from a parameter (with memory address) in JavaScript

I'm trying to understand how the conversion of C code to WebAssembly and the JavaScript interop works in the background. And I'm having problems getting a simple string from a function parameter.

My program is a simple Hello World, and I'm trying to "emulate" a printf/puts.

More or less the C equivalent I want to build:

int main() {
  puts("Hello World\n");
}

You can see a working example here.

My best idea currently is to read 16bit chunks of memory at a time (since wasm seems to allocate them in 16bit intervals) and check for the null-terminaton.

function get_string(memory, addr) {
  var length = 0;

  while (true) {
    let buffer = new Uint8Array(memory.buffer, addr, 16);
    let term = buffer.indexOf(0);

    length += term == -1 ? 16 : term;

    if (term != -1) break;
  }

  const strBuf = new Uint8Array(memory.buffer, addr, length);
  return new TextDecoder().decode(strBuf);
}

But this seems really clumsy. Is there a better way to read a string from the memory if you only know the start address?

And is it really necessary that I only read 16bit chunks at a time? I couldn't find any information if creating an typed array of the memory counts as accessing the whole memory or this only happens when I try to get the data from the array.




Aucun commentaire:

Enregistrer un commentaire