Wasm 源阶段导入


【Wasm Source Phase Imports】

稳定性: 1.2 - 发布候选版

源阶段导入 提案允许使用 import source 关键字组合直接导入 WebAssembly.Module 对象,而不是获取已经用其依赖实例化的模块实例。

【The Source Phase Imports proposal allows the import source keyword combination to import a WebAssembly.Module object directly, instead of getting a module instance already instantiated with its dependencies.】

当需要为 Wasm 进行自定义实例化时,这非常有用,同时仍可以通过 ES 模块集成来解析和加载它。

【This is useful when needing custom instantiations for Wasm, while still resolving and loading it through the ES module integration.】

例如,要创建一个模块的多个实例,或将自定义导入传入新的 library.wasm 实例中:

【For example, to create multiple instances of a module, or to pass custom imports into a new instance of library.wasm:】

import source libraryModule from './library.wasm';

const instance1 = await WebAssembly.instantiate(libraryModule, importObject1);

const instance2 = await WebAssembly.instantiate(libraryModule, importObject2); 

除了静态源阶段之外,还有一种源阶段的动态变体,通过 import.source 动态阶段导入语法实现:

【In addition to the static source phase, there is also a dynamic variant of the source phase via the import.source dynamic phase import syntax:】

const dynamicLibrary = await import.source('./library.wasm');

const instance = await WebAssembly.instantiate(dynamicLibrary, importObject);