如何将 WebAssembly 函数导入 TypeScript?
How to import WebAssembly functions to TypeScript?
我有一个用 TypeScript 编写的现有项目,我正在尝试导入 WebAssembly 模块来替换某些功能。
通过提取将 .wasm 加载到 .js 文件的逻辑,我成功地导入了 WebAssembly 模块。这是它自己的 TypeScript 模块,并被导入到我想使用 WebAssembly 函数的 .ts 文件中。
出于演示目的,我在 wasm 中制作了一个简单的添加函数。
在使用 AssemblyScript 编译为 .wasm 的 .ts 中:
export function add(a: i32, b: i32): i32 {
return a + b;
}
在 .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;
}
在我想使用 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));
});
但是当 运行 这给了我以下错误:
Uncaught (in promise) TypeError: addFunc is not a function at eval
有人知道是什么原因造成的吗?
试试这个片段:
loadWasm().then(module => {
const { add: addFunc } = module.instance.exports;
console.log(addFunc(2, 5));
});
这是一个使用 AssemblyScript Loader 的方法,您可以直接在 TypeScript 中使用:
它需要 "regenerator-runtime": "^0.13.2" 你可以将它和 loader 一起导入到你想要使用 Wasm 模块的 .ts 文件中,因此:
import { instantiateStreaming, ASUtil } from 'assemblyscript/lib/loader';
import { regeneratorRuntime } from 'regenerator-runtime';
我已经像这样实例化了它:
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;
}
之后您可以简单地:
getWasm().then((module) => {
console.log('The result is: ', module.add(3, 4));
});
以及使用加载程序提供的任何附加功能:
let str = "Hello World!";
let ref = module.__retain(module.__allocString(str));
console.log(module.__getString(ref));
我有一个用 TypeScript 编写的现有项目,我正在尝试导入 WebAssembly 模块来替换某些功能。
通过提取将 .wasm 加载到 .js 文件的逻辑,我成功地导入了 WebAssembly 模块。这是它自己的 TypeScript 模块,并被导入到我想使用 WebAssembly 函数的 .ts 文件中。
出于演示目的,我在 wasm 中制作了一个简单的添加函数。
在使用 AssemblyScript 编译为 .wasm 的 .ts 中:
export function add(a: i32, b: i32): i32 {
return a + b;
}
在 .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;
}
在我想使用 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));
});
但是当 运行 这给了我以下错误:
Uncaught (in promise) TypeError: addFunc is not a function at eval
有人知道是什么原因造成的吗?
试试这个片段:
loadWasm().then(module => {
const { add: addFunc } = module.instance.exports;
console.log(addFunc(2, 5));
});
这是一个使用 AssemblyScript Loader 的方法,您可以直接在 TypeScript 中使用:
它需要 "regenerator-runtime": "^0.13.2" 你可以将它和 loader 一起导入到你想要使用 Wasm 模块的 .ts 文件中,因此:
import { instantiateStreaming, ASUtil } from 'assemblyscript/lib/loader';
import { regeneratorRuntime } from 'regenerator-runtime';
我已经像这样实例化了它:
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;
}
之后您可以简单地:
getWasm().then((module) => {
console.log('The result is: ', module.add(3, 4));
});
以及使用加载程序提供的任何附加功能:
let str = "Hello World!";
let ref = module.__retain(module.__allocString(str));
console.log(module.__getString(ref));