From 6f7a9963418ea27d67a9482d3ed229cc611d47c6 Mon Sep 17 00:00:00 2001 From: Cheng Shao Date: Mon, 11 Mar 2024 07:27:23 +0000 Subject: [PATCH] Handle `_initialize` non existence in wasi reactor modules The `_initialize` export is optional and may not exist in a wasi reactor module, according to https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md#current-unstable-abi. When it doesn't exist, simply don't do anything. This is especially useful in cases where a module has been pre-initialized by `wizer` and the `_initialize` export has been stripped. --- src/wasi.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wasi.ts b/src/wasi.ts index 066a8d0..abb4e33 100644 --- a/src/wasi.ts +++ b/src/wasi.ts @@ -43,10 +43,12 @@ export default class WASI { /// Initialize a WASI reactor initialize(instance: { - exports: { memory: WebAssembly.Memory; _initialize: () => unknown }; + exports: { memory: WebAssembly.Memory; _initialize?: () => unknown }; }) { this.inst = instance; - instance.exports._initialize(); + if (instance.exports._initialize) { + instance.exports._initialize(); + } } constructor(