Skip to content

feat(ts): translate an array's map, each_ref, and each_mut - #310

Merged
kixelated merged 3 commits into
mainfrom
quest/m3/ts-array-map
Oct 30, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m3/ts-array-map

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

<[T; N]>::map was refused at its span as an unsupported call, for every element type
(quest/m3/ts-array-map.md, found by #302).
Its stable siblings each_ref and each_mut were refused the same way.

Approach

  • map becomes rt.arrayMap, or rt.bytesMap for a [u8; N] result, which is a
    Uint8Array as array::from_fn's is. Each calls the closure on each element in order.
  • map moves each element into its closure, so discards lists it as discarding nothing.
    A mapping that discards nothing moves every by-value operand into its result or a
    closure, so a closure that owns a value that drops can drop after the call
    (rt.dropAfter) even when the elements drop too. No existing &[] entry takes a
    closure, so nothing else changes.
  • each_ref and each_mut lend each element as first_mut does: an element JS copies
    as its rt.Place (rt.arrayPlaces), an array of bytes as its rt.ByteSlice, and an
    object as itself (rt.arrayFrom).
  • Differential tests against native wasm32:
    • lang arrays::maps: [u8; N] sources and results in both directions, N = 0,
      captures by reference (order seen) and by move, Option<u8> and
      Option<Option<u8>> / Option<()> elements whose None is rt.None, arrays of
      arrays changed in the closure and in the result, and a const-generic length.
    • lang arrays::lent: each_mut of bytes, u16s, strings, byte arrays, and other
      arrays, writes landing in the array; each_ref of each; empty arrays.
    • task drops::closures::iterated: an array of guards mapped by a closure that owns
      a guard. Native and TS logs match: elements drop inside the calls, the closure after
      the last, and what it kept when the result drops.
  • A mutation check (reversed arrayMap order, detached each_mut places) made the lang
    differential report 360 differences.

Impact

  • Translator output only: <[T; N]>::map, each_ref, and each_mut now translate.
  • Runtime: adds rt.arrayMap, rt.bytesMap, rt.arrayFrom, rt.arrayPlaces.

Alternatives

  • Passing the closure's dropper to the runtime function (discards naming the closure)
    instead of relaxing called_closures for mappings that discard nothing. Rejected:
    more runtime surface for the same order.

Not covered

  • Drop order when the closure panics mid-map: a panic traps on both sides, so the
    differential harness can't observe it.
  • try_map, zip, and the split_array_* methods are unstable, so they stay refused at
    their span.

Follow-ups

None.

Review

  • Harness: quest review, Codex gpt-6.1-sol, medium effort.
  • Round 1 on ece4e3ad: no findings.
  • Round 2 on 2fddea5f50d44292b38f35f113f8ce0ba04354e7: no findings. That head merges origin/main (feat(ts): translate zero-sized values MIR never assigns #304) with a hand-resolved conflict in the lang harness's case list.
  • just ci passed locally on 2fddea5f.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 3 commits October 30, 2026 14:42
Quest-Claim: 09b3365f28c1927e
`<[T; N]>::map` calls its closure on each element in order, into an array or, of bytes,
a Uint8Array. It moves each element into the closure and discards none, so elements that
drop are allowed and a closure owning a value that drops drops after the last call.
`each_ref` and `each_mut` lend each element as `first_mut` does: one JS copies as its
place, an array of bytes as its view, and an object as itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated kixelated changed the title An array's map feat(ts): translate an array's map, each_ref, and each_mut Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 22:32
@kixelated
kixelated merged commit d02a3b4 into main Oct 30, 2026
1 of 2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-array-map branch October 30, 2026 22:32
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