refactor(hmr): unify the Swift<->JS scalar crossing behind one JSScalar type#150
Merged
Conversation
…ar type Closes Part I Wave 3 #4 of the arch+DX audit. The "which JS primitive is this" rule was re-derived in three JavaScriptKit-only paths (encodeStateMapToJS, HMRBridge.decodeStateMap, JSAdapter's PatchPayload.Field marshalling) plus the parallel PropertyValue taxonomy — with the SUBTLE rules (Bool-before-Int, HMRNilSentinel<->null, integral-number) living where no unit test could reach them. That's the exact gap the "Blocker 2/3" HMR coercion bugs shipped through. Introduces a closed core `JSScalar` enum (string/int/double/bool/null) split so the fragile classification is HOST-TESTABLE: - core `JSScalar(stateValue:)` / `.stateValue` carry the @State-snapshot rules, incl. Bool-before-Int and HMRNilSentinel<->null; - `PropertyValue.jsScalar` maps DOM properties to the same taxonomy; - a thin, wasm-only `JSScalar(jsValue:)` / `.jsValue` crossing (SwiflowDOM) holds the integral-number->Int rule, keeping Swiflow core free of JavaScriptKit. The three crossings now route through it (four hand-rolled mappings -> one). `_hmrCoerce` is left as-is: on inspection it's a different concern (decoded-Int -> Double-@State target coercion), not a JS crossing — noted in code. Behavior-preserving: every existing HMR round-trip test still passes (they simulate decodeStateMap's output, which JSScalar reproduces exactly). Adds 6 host tests pinning the previously-untested Any<->scalar coercion, so a classification slip is now a unit-test failure, not silent HMR corruption. Verified: host suite 1367/274, HelloWorld wasm build clean, counter e2e 11/11 (real-browser property marshalling via JSScalar.jsValue). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📦 Bundle size
✅ Within budget (≤5% growth allowed). Baseline: Swift 6.3, WASM SDK 6.3-RELEASE, measured 2026-06-18. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes Part I Wave 3 #4 of the arch+DX audit. The "which JS primitive is this" rule was re-derived in three JavaScriptKit-only paths —
encodeStateMapToJS,HMRBridge.decodeStateMap, andJSAdapter'sPatchPayload.Fieldmarshalling — alongside a parallelPropertyValuetaxonomy. The subtle rules (Bool-before-Int,HMRNilSentinel↔null, integral-number→Int) lived where no unit test could reach them — the exact gap the "Blocker 2/3" HMR coercion bugs shipped through.Approach — unify and close the coverage gap
A closed core
JSScalarenum (string/int/double/bool/null), split so the fragile classification becomes host-testable:JSScalar(stateValue:)/.stateValue— the@State-snapshot side, carrying the Bool-before-Int ordering (Bool bridges toNSNumber, soas? Int-first would misclassify it) andHMRNilSentinel↔null;PropertyValue.jsScalar— DOM properties map to the same taxonomy;JSScalar(jsValue:)/.jsValuecrossing in SwiflowDOM (the integral-number→Int rule), keeping Swiflow core free of JavaScriptKit.The three crossings now route through it — four hand-rolled primitive-mappings collapse to one.
_hmrCoerceis left untouched: on inspection it's a different concern (decoded-Int→Double-@Statetarget coercion), not a JS crossing — noted in code.The win beyond de-dup: the previously-untested, previously-buggy classification is now unit-tested, so a coercion slip is a red test rather than silent HMR-state corruption found by hand.
Test plan
Any↔JSScalarcoercion: Bool-before-Int,HMRNilSentinel↔null, unsupported→nil,stateValueround-trip,PropertyValue.jsScalar.decodeStateMap's output, whichJSScalarreproduces byte-for-byte.swift package cleaned the example first for the newJSScalar.swift).JSScalar.jsValue.Note: the wasm
JSValue↔JSScalarcrossing itself is still build-only-verified (no WASM test target exists) — but it is now thin and centralized, and the fragile Swift-side rules moved into the host-tested core, a net reduction in untested surface.🤖 Generated with Claude Code