Skip to content

Implement WASI.getImportObject and WASI.initialize in node:wasi (#12755, #28534)#28555

Open
itisrohit wants to merge 3 commits intooven-sh:mainfrom
itisrohit:claude/implement-wasi-getImportObject
Open

Implement WASI.getImportObject and WASI.initialize in node:wasi (#12755, #28534)#28555
itisrohit wants to merge 3 commits intooven-sh:mainfrom
itisrohit:claude/implement-wasi-getImportObject

Conversation

@itisrohit
Copy link

What does this PR do?

This PR implements two missing Node.js compatibility methods in the `node:wasi` module: `initialize()` and `getImportObject()`. This resolves long-standing compatibility issues for WASI Reactor modules and standardizes the instantiation workflow.

Beyond just adding the methods, this PR addresses a critical instance-safety bug in the original implementation:

  • The Bug: The `node:wasi` script was using shared module-level variables (`let fs, bindings`) for its internal operations. Creating multiple `WASI` instances (e.g., for different sandboxed tasks) would result in state pollution across all instances.
  • The Fix: Refactored the `WASI` class to use instance properties (`this.bindings`) and ensured all internal WASI import closures capture the correct instance state at construction time.

Key Changes:

  • Implemented `WASI.prototype.initialize(instance)`: Correctly handles WASI Reactor modules, validates exports, and sets up memory.
  • Implemented `WASI.prototype.getImportObject()`: Provides the standard import object mapping (`{ [version]: this.wasiImport }`) for `WebAssembly.instantiate()`.
  • Added strict spec guards: `start()` now throws if `_initialize` is present (Command vs. Reactor check), and vice-versa for `initialize()`.
  • Fixed a pre-existing typo (`Recceived` -> `Received`) and several TypeScript lint errors in `wasi.ts`.

How did you verify your code works?

Verification was performed using `bun bd test` with a new comprehensive regression suite and existing WASI tests:

  1. Regression Suite (`test/regression/issue/12755.test.ts`):
    • Verified existence and type of `initialize` and `getImportObject`.
    • Tested dynamic versioning in `getImportObject` (supporting both `preview1` and `unstable`).
    • Instance Isolation Test: Verified that two `WASI` instances with different filesystem bindings do not conflict with each other.
    • Tested strict spec guards for `_start`/`_initialize` overlapping.
  2. Existing Tests:
    • Verified that `test/js/bun/wasm/wasi.test.js` still passes.
  3. Linter & Code Quality:
    • Confirmed `oxlint` passes with 0 warnings/errors.
    • Confirmed `prettier` formatting is consistent with project style.

Tested on macOS with `bun bd`.

Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant