8

I try to understand what the best and most efficient way is to call XmlHttpRequest from WebAssembly.

I found http://webassembly.org/getting-started/js-api/ which seems to explain how to make calls between JavaScript and WebAssembly.

In order for getting this to work it seems to me I have to do the following:

  • Write a JavaScript function that I import into WebAssembly which calls XmlHttpRequest
  • Write a WebAssembly function that I export from WebAssembly which JavaScript calls when the XmlHttpRequest is complete.

In case I want to have a dynamic number of concurrent XmlHttpRequests running, I would also need the imported function to provide a handler that is then provided back by JavaScript to the exported function.

I now have a number of questions:

  1. Is the above accurate and the way to do it?
  2. How do I transfer the URI from WebAssembly to XmlHttpRequest? Do I have to either import or export a WebAssembler.Memory object to/from WebAssembly and place the URI in that?
  3. If the answer to 2 is yes, this WebAssembler.Memory object will be like a global-variable but this can work because there is only one thread. Correct?
  4. Similar to 2, how do I transfer the result of the XmlHttpRequest back to WebAssembly? Also in an imported/exported WebAssembler.Memory object?
  5. In connection with 4, how do I get the result of the XmlHttpRequest into WebAssembly in the most efficient way - e.g. with as few copies as possible? Do I need to copy the result of XmlHttpRequest into the WebAssembler.Memory object from the JavaScript code? And again, this WebAssembler.Memory object is a global variable? I guess I could let the call form WebAssembly to JavaScript pass an index to indicate where in WebAssember.Memory the result should be placed?
user1772004
  • 165
  • 1
  • 6

1 Answers1

3
  1. Yes, this is correct.
  2. You can transfer URIs as strings, as explained in this question about strings
  3. When WebAssembly supports threads, you'd call out to JavaScript and could just heap-allocate the string, pass out its pointer+length, and delete on return from that call.
  4. Yes, transfer it back like a string.
  5. Currently you have to do a copy, though the Community Group is discussing ways that would allow fewer copies to occur in the future. The notes for the latest such discussion are available from the WebAssembly meetings repository.
JF Bastien
  • 6,673
  • 2
  • 26
  • 34
  • Thank you for your answer - this was very useful! A follow up: When compiling from C, I understand there is always a WebAssembly.Memory instance shared between JavaScript and WebAssembly which is the memory used by the C program. Is it possible to share an additional WebAssembly.Memory instance? How would that look from the C side? I think this could be useful so that JavaScript could allocate its own memory for the response from HttpXmlRequest and then pass that to WebAssembly. – user1772004 Nov 03 '17 at 19:04
  • 1
    Currently WebAssembly only supports a single memory. There are plans to support more in the future. It would be up to toolchains to expose it how they see fit. LLVM has IR for `addrspace` which would do this, and some languages use it in LLVM. In C++ allocators were made for something similar (segmentation), but one would have to write code to do this. – JF Bastien Nov 03 '17 at 19:29