Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call XmlHttpRequest from WebAssembly

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?
like image 678
user1772004 Avatar asked Nov 03 '17 06:11

user1772004


1 Answers

  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.
like image 106
JF Bastien Avatar answered Sep 28 '22 08:09

JF Bastien