如何在 Webassembly 中獲取 JSON 文件 (How to Fetch a JSON file in Webassembly)


問題描述

如何在 Webassembly 中獲取 JSON 文件 (How to Fetch a JSON file in Webassembly)

Imports 參數必須存在並且必須是 fetch.then.then.then.module (wasmbyfile.js:64) 處的對象</p>

方法 2:

#include <stdio.h>

include <string.h>

include <emscripten/fetch.h>

/////////////////////////
// This file contains the code for fetching
// ‑> Compiled to .wasm file with emscripten <‑
////////////////////////
void downloadSucceeded(emscripten_fetch_t *fetch) {
printf("Finished downloading %llu bytes from URL %s.\n", fetch‑>numBytes, fetch‑>url);
// The data is now available at fetch‑>data[0] through fetch‑>data[fetch‑>numBytes‑1];
emscripten_fetch_close(fetch); // Free data associated with the fetch.
}

void downloadFailed(emscripten_fetch_t *fetch) {
printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch‑>url, fetch‑>status);
emscripten_fetch_close(fetch); // Also free data on failure.
}

int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE;
attr.onsuccess = downloadSucceeded;
attr.onerror = downloadFailed;
emscripten_fetch(&attr, "./json/bol_list1.json");
}
</code></pre>

錯誤結果:未捕獲(在承諾中)TypeError:WebAssembly 實例化:Imports 參數必須存在並且必須是一個對象

所以我被困在這一點上,並且不確定如何在 JS 中正確加載模塊。我需要一些幫助,還是我從一開始就以錯誤的方式做這件事,有沒有更好的方法來做這件事?

並且不確定如何在 JS 中正確加載模塊。我需要一些幫助,還是我從一開始就以錯誤的方式做這件事,有沒有更好的方法來做這件事?</p> 並且不確定如何在 JS 中正確加載模塊。我需要一些幫助,還是我從一開始就以錯誤的方式做這件事,有沒有更好的方法來做這件事?</p>


參考解法

方法 1:

You are getting an error because your WASM has import statements, while your call to instantiateStreaming does not send an importObject.

But the basic way to use WASM from Javascript is much simpler than: Just define a function in WASM that you can call from JS, and then you do the "fetch" from JS, for instance ("add.wasm"):

(module
  (type $t0 (func (param i32 i32) (result i32)))
  (func $add (type $t0) (param $p0 i32) (param $p1 i32) (result i32)
    get_local $p0
    get_local $p1
    i32.add)
(export "add" (func $add)))

And then call it from Javascript:

const wasmInstanceFromFile = await WebAssembly.instantiateStreaming(await fetch('add.wasm'));
let sum = wasmInstanceFromFile.instance.exports.add(1,2);

(by Sven0567Adi Levin)

參考文件

  1. How to Fetch a JSON file in Webassembly (CC BY‑SA 2.5/3.0/4.0)

#fetch #emscripten #webassembly






相關問題

mysql fetch 中的 $row 是什麼類型的變量? (What kind of variable is $row from a mysql fetch?)

如何在 PHP 中使用 bind_params 正確獲取數據到數組 (How to correctly fetch data to array with bind_params in PHP)

使用 JavaMail 加速電子郵件檢索 (Speed up email retrieval with JavaMail)

使用 eclipselink 從多個表中獲取數據 (Fetch data from multiple tables using eclipselink)

如何在 Webassembly 中獲取 JSON 文件 (How to Fetch a JSON file in Webassembly)

mysqli(a,b,c,d)->query(sql)->fetch_assoc() 究竟是如何工作的? (How exactly does mysqli(a,b,c,d)->query(sql)->fetch_assoc() work?)

如何在 .map 中返回異步函數? (How do I make an asynchronous function return in .map?)

如何僅在響應中的 onclick 事件後映射 json 列表? (How to map a json list only after an onclick event in react?)

在第二個 API 中使用來自第一個 API 的信息 - React (Using information from the first API in the second one - React)

使用 Jest 測試框架調用 fetch(url) 時如何根據 url 模擬變量響應? (How to mock variable responses based on the url when fetch(url) is called using the Jest testing framework?)

函數不返回對像以供 .then 使用 - 但能夠 console.log 對象,並明確返回它 (Function not returning object for .then to work with - but able to console.log the object, and am explicitly returning it)

fetch api - 400 加載資源失敗 (fetch api - 400 failed to load resource)







留言討論