如何在 React-Native 中加载 .wasm 文件?

Jor*_*gen 14 javascript react-native webassembly

我一直在尝试加载一个 WebAssembly (.wasm) 文件——生成的 C++ 代码由 Emscripten 编译为 WebAssembly——在 React-Native 应用程序中。

这是我获取 .wasm 文件的代码:

import fs from 'react-native-fs';

if (!global.WebAssembly) {
  global.WebAssembly = require('webassemblyjs');
}

const fetchWasm = async () => {
  const wasm = await fetch(`${fs.MainBundlePath}/calculator.wasm`);

  console.log(wasm);
  // Returns: Response {type: "default", status: 200, ok: true, statusText: undefined, headers: Headers, …}

  const mod = await WebAssembly.instantiateStreaming(wasm);

  console.log(mod);
  // Throws: TypeError: Failed to execute 'compile' on 'WebAssembly': An argument must be provided, which must be a Response or Promise<Response> object
};
Run Code Online (Sandbox Code Playgroud)

我尝试了我能找到的 Google 搜索结果中的所有内容,但到目前为止没有任何效果。不幸的是,最相关的问题没有得到解答。

有没有人知道我做错了什么?任何帮助将不胜感激!

Asa*_*nka 1

我假设 React-native JS 运行时 JSC 仍然不支持 Android 构建中的 WebAssembly,问题仍然存在 https://github.com/react-native-community/jsc-android-buildscripts/issues/113

另一种方法是使用 Webview,但我认为这不是您所期望的。 https://github.com/ExodusMovement/react-native-wasm