Skip to content

fix(wasm): object patterns bind from their source (#14733) - #14741

Merged
TheHypnoo merged 2 commits into
PerryTS:mainfrom
raliclo:fix/14733-wasm-forof-destructure
Nov 16, 2026
Merged

TheHypnoo merged 2 commits into
PerryTS:mainfrom
raliclo:fix/14733-wasm-forof-destructure

Conversation

@raliclo

@raliclo raliclo commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On --target web, every object pattern read its properties from undefined: const { k } = v and for (const { k } of arr) threw Cannot read properties of undefined (reading 'k'). The HIR opens each object pattern with the __perry_runtime.requireObjectCoercible(source, offset) intrinsic; the wasm backend had no arm for it, so it fell through to the generic native-call path, which skips the arguments and yields undefined. This adds the missing arm.

Changes

  • crates/perry-codegen-wasm/src/emit/expr/iterator_records.rs: requireObjectCoercible emits the new rt_requireObjectCoercible bridge with the source only (the second operand is a byte offset for native --debug-symbols).
  • crates/perry-codegen-wasm/src/wasm_runtime.js: rt_requireObjectCoercible returns its value, or throws TypeError: Cannot convert undefined or null to object (native's message) for null/undefined.
  • crates/perry-codegen-wasm/src/emit/tests.rs: object_patterns_bind_from_their_source_and_reject_nullish covers the for-of head, a nested declaration pattern, a source evaluated once, and a catchable TypeError for null/undefined.

Related issue

Fixes #14733

Test plan

Cause, verified before the fix (main 64257c3, the wasm harness run_web_source):

program main --target web
for (const x of arr) console.log("k", x.k) (no pattern) k 1 / k 2
for (const [a, b] of arr) (array pattern) correct
const { k } = v (declaration, no for-of) TypeError ... (reading 'k')
for (const { k } of arr) TypeError ... (reading 'k')

So the failure follows the object pattern, not for...of; the HIR of the failing programs carries requireObjectCoercible, which no wasm emit arm matches.

  • cargo test -p perry-codegen-wasm --lib object_patterns_bind: passes; with only the two source files reverted (test kept) it fails with TypeError: Cannot read properties of undefined (reading 'k').
  • cargo test --lib --bins -p perry-codegen-wasm and cargo test --bins -p perry (what ./scripts/test_affected_crates.sh --base upstream/main --dry-run lists): perry-codegen-wasm 77 passed (the new test plus 76 existing). perry --bins fails 4 tests (static_require_transform, generated_native_namespaces, library_search, macos_bundle), the same 4 by name as on a pristine main checkout.
  • cargo build-perry --release && scripts/gc_call_effects/sidecars.sh target/release, npm ci, scripts/build_instr_runtime.sh, then A/B against a pristine main build:
    • python3 scripts/parity.py run <subject> --mode web for language/array, language/destructuring, language/function, language/iterator: 0 cases pass on main and fail here. Newly passing: language/destructuring/test_edge_destructuring, test_issue_554_nested_destructure_for_of, test_issue_express_value_not_fn (destructuring web: 7 failing on main, 4 here). Two language/array cases (test_array_reps_loop_base_gc, test_gap_array_fill_3m_objects) also flipped, but they were run-timeouts (exit 124) on the loaded main arm, so I don't count them.
    • bash tests/integration/wasm/run_wasm_tests.sh: 30 passed, 3 failed on both sides (04_functions, 10_higher_order, 19_ffi_imports, the same three as on main).
  • rustfmt --check clean on the touched files; cargo clippy -p perry-codegen-wasm --all-targets: 282 warnings on both sides, none on a touched line.
  • BASE_SHA=upstream/main scripts/run_lint_gates.sh: the FAIL set (24 gates) is identical to a pristine main checkout's. The changelog fragment is added with the PR number.

Checklist

  • I have NOT bumped the workspace version or edited CHANGELOG.md (maintainer handles these at merge)
  • My commits follow the loose feat: / fix: / docs: / chore: prefix convention used in the log
  • I've read CONTRIBUTING.md and agree to the Code of Conduct

🤖 Generated with Claude Code

https://claude.ai/code/session_01CUQanVsnitcrmYLAWWPc3p

Summary by CodeRabbit

  • Bug Fixes
    • Object destructuring in WebAssembly-targeted builds now correctly handles non-nullish values, including array elements and nested object properties, and throws a native-style TypeError for null or undefined.
    • Destructuring evaluates its source expression only once.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Walkthrough

The WASM web target now checks object-pattern sources for null or undefined through a runtime bridge. The bridge returns other values unchanged. A test covers iteration, nested bindings, single source evaluation, and nullish-source errors.

Changes: WASM object-pattern coercion

Layer / File(s) Summary
Emit and validate object coercibility
crates/perry-codegen-wasm/src/emit/expr/iterator_records.rs, crates/perry-codegen-wasm/src/wasm_runtime.js, crates/perry-codegen-wasm/src/emit/tests.rs, changelog.d/14741-wasm-object-pattern-coercible.md
The emitter maps requireObjectCoercible to a runtime bridge. The bridge throws for null or undefined and returns other values unchanged. The test covers object-pattern bindings, source evaluation, and nullish errors. The changelog describes the behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: thehypnoo

Merge Risk: Minimal · up to ba572

This change fixes object destructuring on the web target so patterns bind from their source and null or undefined sources throw a catchable TypeError. The change is small and covered by a targeted test, so merge-readiness risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(wasm): object patterns bind from their source (#14733)" matches the main change. The PR adds a wasm emit arm for the requireObjectCoercible intrinsic, so object destructuring no longer …
Description check Passed The description follows the template. It has a Summary, a Changes list with file paths, a Related issue (Fixes #14733), and a Test plan with detailed commands and results. The author checked the check…
Linked Issues check Passed Issue #14733 requires that for (const { k } of arr) on --target web bind k and log k 1 and k 2, without throwing. The HIR opens each object pattern with `__perry_runtime.requireObjectCoercib…
Out of Scope Changes check Passed All changes connect to the objective. iterator_records.rs adds the emit arm and bridge registration, and its module doc comment is updated to describe the new check. wasm_runtime.js adds the runti…
Full details: Title check

Explanation

The title "fix(wasm): object patterns bind from their source (#14733)" matches the main change. The PR adds a wasm emit arm for the requireObjectCoercible intrinsic, so object destructuring no longer reads from undefined. The title is specific and follows the repository's fix: prefix convention.

Full details: Description check

Explanation

The description follows the template. It has a Summary, a Changes list with file paths, a Related issue (Fixes #14733), and a Test plan with detailed commands and results. The author checked the checklist items. The template's HTML comment about not bumping the version and not editing CHANGELOG.md is not an issue, because the changelog fragment is a new file and the author did not edit CHANGELOG.md. The description also explains the root cause and gives before/after evidence.

Full details: Linked Issues check

Explanation

Issue #14733 requires that for (const { k } of arr) on --target web bind k and log k 1 and k 2, without throwing. The HIR opens each object pattern with __perry_runtime.requireObjectCoercible. At the reviewed head, iterator_records.rs adds a ("requireObjectCoercible", [value, ..]) arm. It emits the new rt_requireObjectCoercible bridge with the source only, so the call no longer falls through to the generic native-call path that yielded undefined. The bridge is registered in BRIDGES. Per the PR description, wasm_runtime.js returns the value unchanged and throws a TypeError for null/undefined. The test object_patterns_bind_from_their_source_and_reject_nullish covers the for-of head and nested declaration patterns. The PR reports that the test fails when the two source files are reverted. The wasm-side bridge body in wasm_runtime.js was not inspected directly. The for-of and declaration cases are fixed by the same arm. No non-coding requirements apply.

Full details: Out of Scope Changes check

Explanation

All changes connect to the objective. iterator_records.rs adds the emit arm and bridge registration, and its module doc comment is updated to describe the new check. wasm_runtime.js adds the runtime bridge. emit/tests.rs adds the regression test. changelog.d/14741-wasm-object-pattern-coercible.md adds a changelog fragment. This is a fragment file, not CHANGELOG.md, which the PR checklist says it does not edit. The PR makes no version bump and no other unrelated edits appear in the summary.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 07b1493e-c375-407d-b429-b0241fdb3e3b
Commits

Reviewing files that changed from the base of the PR and between 64257c3 and 55380a9.

Files selected for processing: 4

  • changelog.d/14741-wasm-object-pattern-coercible.md
  • crates/perry-codegen-wasm/src/emit/expr/iterator_records.rs
  • crates/perry-codegen-wasm/src/emit/tests.rs
  • crates/perry-codegen-wasm/src/wasm_runtime.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@raliclo

raliclo commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Overlap note: #14664 (ours) carries the same rt_requireObjectCoercible bridge and requireObjectCoercible arm, in commit 1e1229d. #14664 needed it because, once a closure called through an object property actually runs, every const { x = d } = obj inside it hit the same missing arm.

This PR is the standalone fix for #14733 and doesn't depend on #14664. Whichever lands first, I'll drop the duplicate hunk from the other when I rebase it.

TheHypnoo pushed a commit to raliclo/perry that referenced this pull request Nov 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CUQanVsnitcrmYLAWWPc3p
@TheHypnoo
TheHypnoo force-pushed the fix/14733-wasm-forof-destructure branch from 55380a9 to d97da07 Compare November 16, 2026 13:56
TheHypnoo pushed a commit to raliclo/perry that referenced this pull request Nov 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CUQanVsnitcrmYLAWWPc3p
@TheHypnoo
TheHypnoo force-pushed the fix/14733-wasm-forof-destructure branch from d97da07 to ba572a0 Compare November 16, 2026 15:25
ralic and others added 2 commits November 16, 2026 16:28
Every object pattern opens with the HIR intrinsic
`__perry_runtime.requireObjectCoercible(source, offset)`. The wasm
backend had no arm for it, so it fell through to the generic native-call
path, which drops the arguments and yields `undefined`: `const { k } = v`
and `for (const { k } of arr)` on `--target web` read `k` from
`undefined` and threw `Cannot read properties of undefined (reading 'k')`.

The wasm backend now emits the check as the `rt_requireObjectCoercible`
bridge, which returns the source or throws the TypeError native throws
for a nullish one.

Fixes PerryTS#14733

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CUQanVsnitcrmYLAWWPc3p
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CUQanVsnitcrmYLAWWPc3p
@TheHypnoo
TheHypnoo force-pushed the fix/14733-wasm-forof-destructure branch from ba572a0 to 5ee4028 Compare November 16, 2026 15:28
@TheHypnoo
TheHypnoo merged commit a9e3b11 into PerryTS:main Nov 16, 2026
5 checks passed
@raliclo
raliclo deleted the fix/14733-wasm-forof-destructure branch November 17, 2026 01:36
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.

wasm/web: destructuring in a for...of head throws (for (const { k } of arr))

3 participants