简体   繁体   English

如何将WebAssembly函数导入TypeScript?

[英]How to import WebAssembly functions to TypeScript?

I have an existing project written in TypeScript and I'm trying to import a WebAssembly Module to replace some functionality. 我有一个用TypeScript编写的现有项目,我正在尝试导入WebAssembly模块以替换某些功能。

I have managed to successfully import the WebAssembly module by extracting the logic for loading the .wasm to a .js file. 通过提取将.wasm加载到.js文件的逻辑,我已经成功地成功导入了WebAssembly模块。 This is it's own TypeScript module and is imported into the .ts file where I want to use the WebAssembly functions. 这是它自己的TypeScript模块,并导入到要使用WebAssembly函数的.ts文件中。

For demonstration purposes I have made a simple add function in wasm. 为了演示,我在wasm中做了一个简单的添加功能。

In the .ts that is compiled to .wasm with AssemblyScript: 在使用AssemblyScript编译为.wasm的.ts中:

export function add(a: i32, b: i32): i32 {
  return a + b;
}

In the .js file: 在.js文件中:

export async function loadWasm() {
  const imports = {}; // Omitted the contents since it's most likely irrelevant
  const module = await 
  WebAssembly.instantiateStreaming(fetch('path/to/file.wasm'),imports);
  return module;
}

And in the .ts file where I want to use the WebAssembly: 在要使用WebAssembly的.ts文件中:

loadWasm().then((module: any) => {
  let addFunc: ((a: number, b: number) => any) = module.add;
  console.log('Adding 2 and 5 in Wasm: ' + addFunc(2, 5));
});

But when running this it gives me the following error: 但是运行此命令时,出现以下错误:

Uncaught (in promise) TypeError: addFunc is not a function at eval

Does anyone know what causes this? 有谁知道是什么原因造成的?

Try this snippet: 试试以下代码片段:

loadWasm().then(module => {
  const { add: addFunc } = module.instance.exports;
  console.log(addFunc(2, 5));
});

Full example in WebAssembly Studio WebAssembly Studio中的完整示例

Here's a method using AssemblyScript Loader that you can use directly in the TypeScript: 这是一种使用AssemblyScript Loader的方法,您可以直接在TypeScript中使用它:

It requires "regenerator-runtime": "^0.13.2" which you can import together with the loader in the .ts file where you want to use the Wasm module, as such: 它需要“ regenerator-runtime”:“ ^ 0.13.2” ,您可以将其与加载程序一起导入到要使用Wasm模块的.ts文件中,例如:

import { instantiateStreaming, ASUtil } from 'assemblyscript/lib/loader';
import { regeneratorRuntime } from 'regenerator-runtime';

I've instantiated it like so: 我已经实例化了:

interface MyApi {
    add(a: number, b: number): number;
}

async function getWasm(): Promise<ASUtil & MyApi> {
    const imports: any = {};
    let module: ASUtil & MyApi = await instantiateStreaming<MyApi>(fetch('path/to/file.wasm'), imports);
    return module;
}

After which you can simply: 之后,您可以简单地:

getWasm().then((module) => {
    console.log('The result is: ', module.add(3, 4));
});

As well as use any of the additional functionality that the Loader provides: 以及使用加载程序提供的任何其他功能:

let str = "Hello World!";
let ref = module.__retain(module.__allocString(str));
console.log(module.__getString(ref));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM