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:
- Is the above accurate and the way to do it?
- 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?
- 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?
- Similar to 2, how do I transfer the result of the XmlHttpRequest back to WebAssembly? Also in an imported/exported WebAssembler.Memory object?
- 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?