Wasm 源阶段导入


¥Wasm Source Phase Imports

源阶段导入 提案允许使用 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);