mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.12k stars 22.46k forks source link

Explanation for WASM return statement has unclear formulation on behavior when stack contains more values than return type #35264

Open wucke13 opened 1 month ago

wucke13 commented 1 month ago

MDN URL

https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow/return

What specific section or headline is this issue about?

return

What information was incorrect, unhelpful, or incomplete?

The statement reads:

If there are more values that the function's return type specifies, then the excess values are popped from the stack and discarded, and the last N values are returned.

Assuming that we speak of a stack implemented by a vector, then I assume pop refers to consuming/removing the last element of the vec. Applying this understanding and going through the sentence step by step, I come up with the following (wrong) step-by-step:

  1. Pop values from stack until stack length equals number of return values for the given function (denoted N)
  2. Return the remaining N values from the stack

However, this would yield 10, while the example code is supposed to yield 90.

What did you expect to see?

If there are more values than specified by the function's return type, then the topmost N values are returned, and all other values on the stack belonging to the current functions call-frame are removed.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

If there are more values thatthan the function's return type specifies, then the excess values are popped from the stack and discarded, and the last N values are returned.

MDN metadata

Page report details * Folder: `en-us/webassembly/reference/control_flow/return` * MDN URL: https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow/return * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/webassembly/reference/control_flow/return/index.md * Last commit: https://github.com/mdn/content/commit/c681ed89305afd56d54ba6671673680bea041670 * Document last modified: 2023-11-03T06:30:13.000Z
Josh-Cena commented 1 month ago

Yeah, I think we should say: the topmost N elements are returned, while the rest are discarded. See spec: https://webassembly.github.io/spec/core/exec/instructions.html#xref-syntax-instructions-syntax-instr-control-mathsf-return