Skip to content

Comments

BridgeJS: Fix stack ordering for optional arrays and dictionaries#671

Merged
krodak merged 1 commit intoswiftwasm:mainfrom
PassiveLogic:kr/fix-stack-ordering
Feb 22, 2026
Merged

BridgeJS: Fix stack ordering for optional arrays and dictionaries#671
krodak merged 1 commit intoswiftwasm:mainfrom
PassiveLogic:kr/fix-stack-ordering

Conversation

@krodak
Copy link
Member

@krodak krodak commented Feb 21, 2026

Overview

Follow-up to #472 which fixed LIFO stack ordering for multiple stack-based parameters but missed some types.

The generateParameterLifting() method in ExportSwift.swift handles the case where multiple stack-based parameters appear in a single function - it extracts their pops into reverse-ordered temporaries so each parameter reads its own data from the shared LIFO stacks. The original fix covered .swiftStruct, .associatedValueEnum, and .array, but missed .dictionary and any .nullable() wrapper around stack-based types (e.g. [Int]?, [String: V]?).

This caused a runtime crash when a function had two or more optional array parameters - the first parameter would pop the second parameter's data, leading to type confusion and a wasm trap.

1. Replace hardcoded type matching with isStackUsingParameter

Added a computed property on BridgeType that recursively identifies stack-using types, including through nullable wrappers. This replaces the incomplete switch statement and won't break when new stack-based types are added.

Tests

Added multiArrayParams(nums:strs:), multiOptionalArrayParams(a:b:), and a MultiArrayContainer class with a multi-array constructor, plus E2E assertions verifying each parameter receives its own data.

@krodak krodak force-pushed the kr/fix-stack-ordering branch from 158b64e to 9339c1c Compare February 21, 2026 23:59
@krodak krodak self-assigned this Feb 22, 2026
@krodak krodak merged commit aa9aa77 into swiftwasm:main Feb 22, 2026
12 checks passed
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.

2 participants