fix(wasm): #1081 — invalid stack balance on Stmt::Return of void call - #1086
Merged
Conversation
`v => console.log(v)` lowers to a closure with body `Stmt::Return(Some(console.log(v)))`. The closure's WASM signature is `[i64] -> [i64]`, but emit_expr for the console.* call emits `emit_memcall_void` + early `return` from the match arm, leaving the operand stack empty. The subsequent `Instruction::Return` then fails validation with "expected 1 elements on the stack for return, found 0" — the V8 error from #1081. Mirror JS semantics (a void call evaluates to `undefined`) by pushing TAG_UNDEFINED when `expr_has_value(e)` is false in `Stmt::Return(Some(e))`. This is the same pattern `Stmt::Expr` already uses (Drop only when expr_has_value). Regression test: tests/wasm/22_return_void_expr.ts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1081.
Summary
.wasmwithexpected 1 elements on the stack for return, found 0. The trigger is an expression-bodied arrow whose body is a void call, e.g.v => console.log(v).Stmt::Return(Some(console.log(v))). The closure's WASM signature is[i64] -> [i64], butemit_exprforconsole.log/warn/error(and otheremit_memcall_voidpaths) returns from the match arm without pushing a result. The subsequentInstruction::Returnthen violates the function signature.undefined) by pushingTAG_UNDEFINEDwhenexpr_has_value(e)is false inStmt::Return(Some(e)). Same patternStmt::Expralready uses (Droponly whenexpr_has_value).Files
crates/perry-codegen-wasm/src/emit.rs— pushTAG_UNDEFINEDafteremit_exprinStmt::Return(Some(e))when the expression is void.tests/wasm/22_return_void_expr.{ts,expected}— regression test coveringg().then(v => console.log(v))andconst log = (v: number) => console.log(v).Test plan
WebAssembly.compileaccepts the produced module (was failing without the fix).tests/wasm/run_wasm_tests.sh: 15 passed / 7 failed; the 7 pre-existing failures matchmainand the new22_return_void_exprpasses.cargo fmt --all;cargo build --release -p perry-codegen-wasm -p perry-runtime -p perry-stdlib -p perry.No version bump or changelog change — maintainer will fold those in at merge time.