Skip to content

fix(ui-wasm): #1392 wire reactive state + setText on web/wasm target#1404

Merged
proggeramlug merged 1 commit into
mainfrom
worktree-fix-1392-wasm-ui-state
May 22, 2026
Merged

fix(ui-wasm): #1392 wire reactive state + setText on web/wasm target#1404
proggeramlug merged 1 commit into
mainfrom
worktree-fix-1392-wasm-ui-state

Conversation

@proggeramlug
Copy link
Copy Markdown
Contributor

Fixes #1392.

Problem

On the web/wasm target (what the playground hub compiles to), perry/ui's reactive state and imperative setText were silent no-ops:

  • count.text() showed the initial value and never changed after set.
  • count.value read undefined.
  • setText(id, value) dispatched to nothing; Text(content, id)'s 2nd id arg was dropped.

Root cause

The state_desugar HIR pass (crates/perry-transform/src/state_desugar.rs) lowers state<T> to a synthetic, string-keyed registry API: __state_init / __state_get / __state_set, the 2-arg Text(content, id) form, plus __foreach_register and __navstack_register_route. The native LLVM backend special-cases these (lower_call/native/mod.rs) and backs them with perry-runtime/src/ui_text_registry.rs. But neither the wasm backend (perry-codegen-wasm) nor the JS backend (perry-codegen-js) implemented them:

  • __state_* resolved to perry_ui_unknown / __perry.perry_ui___state_* (nonexistent).
  • perry_ui_text_create ignored its 2nd id arg.
  • there was no perry_ui_set_text.

Fix

Mirrors the native model — the synth id is reused as both the state key and the Text widget's setText id, so a write fans out to every bound widget.

Emit — map the synthetic methods to keyed bridge functions:

  • crates/perry-codegen-wasm/src/emit/ui_method_map.rs
  • crates/perry-codegen-js/src/emit/calls.rs

Runtime (wasm_runtime.js + web_runtime.js):

  • keyed-state registry (perry_ui_state_init / perry_ui_state_get_keyed / perry_ui_state_set_keyed)
  • perry_ui_set_text(id, value) + an id→widget text registry
  • perry_ui_text_create(text, id) registers the 2-arg form
  • perry_ui_foreach_register / perry_ui_navstack_register_route keyed dispatch
  • all registered in __perryUiDispatch (wasm) / window.__perry (js)

Verification

  • Compiled the issue's counter repro to --target wasm; bridge names now resolve (perry_ui_state_init / _get_keyed / _set_keyed, 2-arg perry_ui_text_create) — no more perry_ui_unknown.
  • End-to-end: booted the compiled counter wasm in Node with a DOM mock — button click fires, bound Text re-renders 0 → 1 → 2, and count.value reads correctly (console.log prints val 1, val 2, not val undefined).
  • Behavioral unit tests over the real wasm_runtime.js and web_runtime.js pass for keyed get/set, reactive Text, 2-arg Text, and imperative setText.
  • cargo fmt --all -- --check clean; perry-codegen-wasm / perry-codegen-js / perry-dispatch / perry-transform tests pass.

Note: version bump + CHANGELOG entry intentionally omitted (folded in at merge per repo workflow).

On the web/wasm target, perry/ui's reactive `state` and imperative
`setText` were silent no-ops: `count.text()` never updated, `count.value`
read `undefined`, and `setText(id, val)` dispatched to nothing.

The `state_desugar` HIR pass lowers `state<T>` to a synthetic, string-keyed
registry API (`__state_init`/`__state_get`/`__state_set`, plus 2-arg
`Text(content, id)`, `__foreach_register`, `__navstack_register_route`).
The native LLVM backend special-cases these in lower_call/native/mod.rs and
backs them with perry-runtime's ui_text_registry, but neither the wasm
backend (perry-codegen-wasm) nor the JS backend (perry-codegen-js)
implemented them — the synthetic methods resolved to `perry_ui_unknown`
and `perry_ui_text_create` dropped its 2nd `id` arg.

Fix mirrors the native model (synth id reused as both the state key and the
Text widget's setText id, so a write fans out to every bound widget):

- emit: map `__state_*` / `__foreach_register` / `__navstack_register_route`
  to keyed bridge fns in both ui_method_map.rs (wasm) and calls.rs (js).
- runtime (wasm_runtime.js + web_runtime.js): add a keyed-state registry,
  `perry_ui_set_text`, keyed get/set/init, foreach + navstack dispatch, and
  make `perry_ui_text_create(text, id)` register the 2-arg form so `setText`
  and `__state_set` find and re-render it.

Verified end-to-end by booting the compiled counter wasm in Node with a DOM
mock: button click fires, bound Text re-renders 0->1->2, and `count.value`
reads correctly.
@proggeramlug proggeramlug merged commit 5a6d082 into main May 22, 2026
9 checks passed
@proggeramlug proggeramlug deleted the worktree-fix-1392-wasm-ui-state branch May 22, 2026 14:32
proggeramlug added a commit that referenced this pull request May 22, 2026
…sweep (#1414)

Rolls up 26 PRs that merged to main post-v0.5.1023 without version
bumps:

- node:crypto gap-fixes (#1386 #1393 #1394 #1402 #1405): randomInt,
  timingSafeEqual, getHashes/getCiphers, sha224/sha384, base64 digest,
  Buffer hash input, no-arg digest() → Buffer, pbkdf2Sync digest arg,
  scryptSync.
- node:perf_hooks (#1321 + #1328 #1342 coverage): performance + User
  Timing + PerformanceObserver native impl, granular node-suite +
  edge-case coverage.
- #1090 GC checkpoint runtime work (#1324).
- #1311 geisterhand on iOS (#1316 #1383 #1384 #1385).
- #1312 process.env.X (unset) is nullish undefined (#1314).
- #1319 thread-safety hardening for cross-thread runtime statics.
- #1322 exact-head GC evidence packet.
- #1323 wasm timers dispatch through mem_call bridge (#1329).
- #1317 node:timers/promises shadow-segfault fix (#1326).
- #1330 node:process suite (#1331).
- #1292 bcrypt.hash() returns String (#1307).
- #1293 fastify .json()/.body external-fastify dispatch (#1308).
- #1296 app pattern performance gaps.
- #1297 diagnostics_channel parity.
- #1301 iOS App Groups capability (#1313).
- #1318 #1325 os/methods/modern-methods static dispatch.
- #1315 expanded Node parity test coverage.
- #1382 ui-ios stdlib pump for async fetch.
- #1392 ui-wasm reactive state + setText (#1404).
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.

web/wasm target: perry/ui reactive state + setText are no-ops (interactive label updates don't work)

1 participant