BridgeJS: Normalize wasm pointer offsets in JS by krodak · Pull Request #768 · swiftwasm/JavaScriptKit
Overview
This PR fixes signed i32 pointer handling at the Swift-to-JavaScript boundary. WebAssembly pointers can legally point above the 2 GiB line, but the JS API exposes i32 values as signed numbers. When generated BridgeJS glue or runtime helpers used those signed values directly as typed-array or DataView offsets, pointers above 0x7fff_ffff could become negative and fail with an out-of-bounds RangeError.
The change normalizes pointer offsets with >>> 0 before using them to index WebAssembly linear memory. It also canonicalizes Swift heap object wrapper identity keys so signed and unsigned representations of the same pointer do not miss the identity cache.
1. BridgeJS generated glue
Generated swift_js_init_memory, swift_js_init_memory_with_result, and released-closure debug paths now unsigned-normalize pointer inputs before creating Uint8Array views. Swift heap object wrappers normalize pointers before storing or looking up identity-cache entries.
2. Runtime helpers
Runtime DataView and typed-array helper paths now unsigned-normalize pointer offsets before reading or writing memory. This covers JSValue array decoding, object-ref decoding, result payload writes, string loading/decoding, and typed-array creation/loading.