乐闻世界logo
搜索文章和话题

How to call XmlHttpRequest from WebAssembly

1个答案

1

WebAssembly itself does not provide built-in capabilities for manipulating the DOM or sending network requests. Instead, you need to invoke these Web APIs via JavaScript and interact with these JavaScript functions from your WebAssembly code.

To call XMLHttpRequest from WebAssembly, you need to perform the following steps:

  1. Create a JavaScript function that wraps the XMLHttpRequest call logic.
  2. Import this JavaScript function into the WebAssembly module so that WebAssembly can call it.
  3. In your WebAssembly code, call the imported JavaScript function to perform the network request.

The following is a simple example demonstrating how to implement the above steps:

JavaScript code

javascript
// Encapsulation of XMLHttpRequest in JavaScript function makeRequest(url, method) { const xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.onload = function () { // Handle response, e.g., pass response back to WebAssembly }; xhr.onerror = function () { // Handle error }; xhr.send(); } // Instantiate WebAssembly module WebAssembly.instantiateStreaming(fetch('your_wasm_module.wasm'), { env: { // Export JavaScript function to WebAssembly jsMakeRequest: function(ptr, len) { // Assume ptr is the pointer to the URL string in WebAssembly memory // len is the length of the URL string const url = new TextDecoder().decode(new Uint8Array(memory.buffer, ptr, len)); makeRequest(url, "GET"); } } }).then(results => { // Use the instantiated WebAssembly module const instance = results.instance; // Assume you have a function in WebAssembly that calls jsMakeRequest instance.exports.callJsMakeRequest(); }).catch(console.error);

WebAssembly code (example using WebAssembly text format)

wat
(module ;; Import JavaScript function (import "env" "jsMakeRequest" (func $jsMakeRequest (param i32 i32))) ;; Import WebAssembly memory object (import "env" "memory" (memory 1)) (func $callJsMakeRequest ;; Assume URL string location and length in memory are known (local $urlPtr i32) (local $urlLen i32) ;; Set URL string location and length (set_local $urlPtr ...) (set_local $urlLen ...) ;; Call imported JavaScript function (call $jsMakeRequest (get_local $urlPtr) (get_local $urlLen)) ) ;; Export $callJsMakeRequest function so JavaScript can call it (export "callJsMakeRequest" (func $callJsMakeRequest)) )

Note that the provided WebAssembly code is illustrative and may require adjustments to syntax and logic based on the specific compiler and environment. In practice, you might program using languages that compile to WebAssembly (e.g., Rust, C, C++), and the compiler will handle generating the imports and exports.

Ensure that the WebAssembly module and JavaScript share the same memory object so that JavaScript functions can read WebAssembly memory to retrieve passed parameters, such as the URL string. The above code does not cover details like error handling and memory management—these aspects should be prioritized in real applications.

2024年6月29日 12:07 回复

你的答案