Skip to content

[js-api] Convert builtin arguments and results like host functions do - #2348

Merged
eqrion merged 1 commit into
WebAssembly:mainfrom
kaist-plrg:fix/js-api-builtin-value-conversions
Nov 24, 2026
Merged

eqrion merged 1 commit into
WebAssembly:mainfrom
kaist-plrg:fix/js-api-builtin-value-conversions

Conversation

@f52985

@f52985 f52985 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Resolves #2330.

create a builtin function now mirrors the behavior of create a host function:
the host function now converts its arguments with ToJSValue and its result with ToWebAssemblyValue. It also turns a throw into a trap, and explicitly takes/returns the store, as in #2326.

It also brings several changes and refactorings:

  • i32 parameters are reinterpreted as unsigned with ToUint32, as the original proposal does with >>>= 0.
  • Some input JSValues become mathematical values on the first line (e.g. Let |startIndex| be ℝ(...|start|...)), and are then used directly in comparisons.
  • Unnecessary Assert and ToJSValue are removed.
  • Return values are now JS Numbers (+0𝔽, -1𝔽, 𝔽(stringLength), ...).
  • The as if a trap was executed phrase is removed, since the host function now explicitly turns a throw into a trap.
  • Reads and writes to an array are now handled through three new abstract operations: ArrayLength, ArrayCharCodeAt, and SetArrayCharCodeAt. These behaviors involve accessing the associated store and type conversion from/to I16, and factoring them out into separate abstract algorithms makes the spec cleaner and more concise.

While updating the test, I found that it was missing some cases and that its polyfill had a few oversights, so I fixed those along the way:

  • Nothing exercised i32 arguments with the top bit set. The existing tests only passed small, non-negative values, where reading an i32 as signed or unsigned gives the same result. I added a test, "i32 parameters are unsigned", that passes -1 and -0x80000000 from JS (i.e. 0xffffffff and 0x80000000) to every i32 parameter, which should be read as large unsigned values.
  • The polyfill's fromCodePoint was wrong: it did not trap above 0x10ffff, unlike the spec.
  • The polyfill's fromCharCodeArray was wrong: its third argument's role is an end index, but it was using it as a count.
    • This went unnoticed because fromCharCodeArray was only called with a start of 0, and in that case, an end index and a count mean the same thing. I added a loop that calls it with every start from 0 to the length.
  • The polyfill's substring condition was wrong: it used the comma operator, so only its last comparison counted, and the rest were ignored. Moreover, the condition end > length was not supposed to be there, which just happened to be ignored. I replaced it with the correct condition (start > end || start > length).

@eqrion eqrion left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, that's a big improvement.

@eqrion
eqrion merged commit 19f9936 into WebAssembly:main Nov 24, 2026
13 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.

[js-api] The type confusion in builtin section

2 participants