Skip to content

WASIp2-via-WASIp3 Adapter#73

Merged
pavelsavara merged 26 commits into
mainfrom
adapter
Apr 21, 2026
Merged

WASIp2-via-WASIp3 Adapter#73
pavelsavara merged 26 commits into
mainfrom
adapter

Conversation

@pavelsavara

@pavelsavara pavelsavara commented Apr 21, 2026

Copy link
Copy Markdown
Owner

Summary

Replaces the standalone WASIp2 host with a layered architecture: a native WASIp3 host plus a WASIp2-via-WASIp3 compatibility adapter. Adds auto-detection of WASI version from component imports/exports.

Motivation

The previous WASIp2 host was a standalone implementation that duplicated logic already present in the WASIp3 host. This PR consolidates both WASI versions under a single WASIp3 foundation, with WASIp2 support provided as a thin adapter layer. This reduces maintenance burden and ensures feature parity between the two versions.

Key Changes

1. WASIp2 → WASIp3 Adapter Architecture

  • Deleted the entire src/host/wasip2/ directory
  • Added src/host/wasip2-via-wasip3/ — a new compatibility shim layer that translates WASIp2 interface calls to the WASIp3 host
  • Extended src/host/wasip3/ with additional implementations and tests

2. Auto-Detection API (instantiateWasiComponent)

New src/wasi-auto.ts provides:

  • WasiType enum — None, P2, P3
  • detectWasiType() — inspects component exports/imports for wasi: prefixes and @0.2 version patterns to determine which WASI version the component targets
  • createWasiImports() — creates the correct host (P3 direct or P2-via-P3 adapter) based on detected type

The top-level instantiateWasiComponent() in src/index.ts now auto-detects and instantiates with the appropriate host.

3. Dynamic Code-Split Loaders

New src/dynamic.ts provides lazy-loaded entry points:

  • loadWasiP3Host() — auto-selects Node.js or browser WASIp3 host
  • loadWasiP2ViaP3Adapter() — loads the P2-via-P3 adapter
  • loadWasiP3Serve() — loads the P3 serve support

5. BindingContext → MarshalingContext Rename

  • BindingContext type moved from src/resolver/model/types.ts to src/marshal/model/types.ts and renamed to MarshalingContext
  • All bctx parameters renamed to mctx throughout resolver and marshal code
  • Backward-compatible re-export alias kept in resolver model
  • New pendingBackgroundTasks: [] field added to support background tasks from sync canon.lower with stream/future params

6. Trampoline-Lower Refactoring

  • Extracted processFlatResult, processSpilledResult, handleLowerResult, handleLowerResultSpilled helper functions
  • All 4 trampoline variants (lowerFlatFlat, lowerFlatSpilled, lowerSpilledFlat, lowerSpilledSpilled) now delegate to these helpers
  • New hasFutureOrStreamReturn flag in lowering plan distinguishes async returns that are future/stream values (should not be awaited) from regular async returns (should be .then()-chained through the result processor)

7. Parser Enhancement

  • Core WASM module detection (WASI P1) with a clear error message when users accidentally pass a .wasm core module instead of a component

8. Build Configuration

  • Rollup config updated: wasip2 bundles replaced with wasip2ViaP3 and wasip2ViaP3Node bundles
  • Property mangling config removed
  • Added directory/index.ts resolution support

9. Minor Changes

  • WasiExit.status renamed to WasiExit.exitCode
  • cliMain() removed from src/utils/args.ts
  • getBuildInfo() simplified to use plain string property names
  • Demo scripts updated to new dist path (./dist/release/index.js)
  • Jest coverage exclusions updated

Breaking Changes

  • createWasiP2Host() removed — use createWasiP3Host() + createWasiP2ViaP3Adapter(), or the new auto-detecting instantiateWasiComponent()
  • WasiExit.statusWasiExit.exitCode
  • Import paths changed: @pavelsavara/jsco/wasip2@pavelsavara/jsco/wasip2-via-wasip3
  • Result values now use plain .tag/.val properties instead of symbol-keyed [TAG]/[VAL]
  • setCreateComponent() removed from WASI instantiation API

@pavelsavara pavelsavara self-assigned this Apr 21, 2026
@github-actions

github-actions Bot commented Apr 21, 2026

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 96.02% 1858 / 1935
🔵 Statements 95% 1996 / 2101
🔵 Functions 96.23% 281 / 292
🔵 Branches 90.68% 974 / 1074
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/wasi-auto.ts 88.23% 83.33% 100% 92.3% 35, 41
src/binder/to-abi.ts 90.67% 83.56% 92.85% 90.76% 36-37, 59, 89-93, 153, 157, 189, 296, 386-410, 505, 512, 516
src/binder/to-js.ts 93.55% 90% 92.3% 93.95% 43-44, 89-92, 150, 154, 186, 317, 412, 417-423, 534, 543, 547-549
src/marshal/constants.ts 100% 100% 100% 100%
src/parser/index.ts 97.82% 96.07% 92.3% 98.86% 52, 180
src/resolver/model/types.ts 100% 100% 100% 100%
src/utils/args.ts 100% 90.76% 100% 100%
src/utils/assert.ts 92% 81.81% 100% 100% 56, 62
src/utils/jspi.ts 83.33% 100% 100% 80% 11
Unchanged Files
src/binder/cache.ts 100% 100% 100% 100%
src/marshal/validation.ts 96.29% 95.45% 100% 96.15% 42
src/parser/alias.ts 100% 100% 100% 100%
src/parser/canon.ts 100% 100% 100% 100%
src/parser/coreInstance.ts 100% 100% 100% 100%
src/parser/export.ts 100% 75% 100% 100%
src/parser/import.ts 100% 100% 100% 100%
src/parser/instance.ts 100% 100% 100% 100%
src/parser/type.ts 100% 100% 100% 100%
src/parser/values.ts 94.7% 94.94% 100% 94.56% 168, 173, 318-361, 582, 589, 599, 602, 1034, 1037, 1053
src/parser/model/aliases.ts 100% 100% 100% 100%
src/parser/model/core.ts 100% 100% 100% 100%
src/parser/model/exports.ts 100% 100% 100% 100%
src/parser/model/instances.ts 100% 100% 100% 100%
src/parser/model/tags.ts 100% 100% 100% 100%
src/parser/model/types.ts 100% 100% 100% 100%
src/resolver/calling-convention.ts 96.19% 91.61% 100% 99.18% 28, 35, 42, 49, 56, 331, 449-450, 492, 495
src/resolver/import-names.ts 100% 100% 100% 100%
src/resolver/type-resolution.ts 93.97% 89.83% 80% 98.66% 23, 85, 93, 95, 110
src/resolver/type-validation.ts 90.66% 87.27% 100% 95.38% 67, 89, 120, 131, 138-141
src/resolver/types.ts 80.95% 75% 100% 80.95% 41-45
src/resolver/model/binding-plan.ts 100% 100% 100% 100%
src/resolver/model/calling-convention.ts 100% 100% 100% 100%
src/utils/shared.ts 100% 100% 100% 100%
src/utils/streaming.ts 97.56% 91.48% 97.29% 99.37% 102, 135, 183, 340
Generated in workflow #265 for commit ffcb001 by the Vitest Coverage Report Action

@pavelsavara pavelsavara changed the title p2->p3 Adapter WASIp2-via-WASIp3 Adapter Apr 21, 2026
@pavelsavara pavelsavara marked this pull request as ready for review April 21, 2026 11:28
@pavelsavara pavelsavara merged commit 311d97f into main Apr 21, 2026
8 checks passed
@pavelsavara pavelsavara deleted the adapter branch April 21, 2026 11:28
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