Commit 30bb265
committed
JSTypedArray: annotate swjs_create_typed_array with bounds-safety attributes
On recent clang/Swift toolchains, bounds-safety annotations on `void *`
parameters cause the Swift importer to synthesize an
`UnsafeRawBufferPointer`-taking overload alongside the original. Adding
these annotations to `swjs_create_typed_array` gives us that overload
for free, and it lays the foundation for broader `Span` adoption in
JavaScriptKit in follow-up PRs.
Because `elements_ptr` is `void *`, the byte count is
`length * sizeof(element)`. This PR adds a trailing `element_size`
parameter and annotates `elements_ptr` as
`__sized_by_or_null(length * element_size) __noescape`. Both annotations
are correct given the JS implementation: it constructs a typed array view
over WASM linear memory at `elementsPtr` with `length` elements and
immediately calls `.slice()` to copy before returning. The view
constructor reads exactly `length * constructor.BYTES_PER_ELEMENT` bytes,
which equals `length * element_size` since Swift passes
`MemoryLayout<Element>.stride` — matching `BYTES_PER_ELEMENT` for every
`TypedArrayElement` type. And because only the `.slice()` copy is
retained and returned, `elementsPtr` never escapes the call.
`__sized_by_or_null` rather than `__sized_by` preserves the existing
nullable contract: Swift's `Array.baseAddress` is `nil` for empty arrays,
and the JS side already handles `length == 0` without dereferencing the
pointer.
Fallback no-op macros (via `<ptrcheck.h>` + `#ifndef` guards) keep the
header portable across toolchains that predate bounds-safety support. The
only coordinated change is in `JSTypedArray.swift`'s `createTypedArray`,
which passes `Int32(MemoryLayout<Element>.stride)` as the new argument;
the JS dispatch entry is unchanged — extra WASM arguments are silently
dropped.1 parent 403ae95 commit 30bb265
2 files changed
Lines changed: 30 additions & 5 deletions
File tree
- Sources
- JavaScriptKit/BasicObjects
- _CJavaScriptKit/include
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
12 | 23 | | |
13 | 24 | | |
14 | 25 | | |
| |||
291 | 302 | | |
292 | 303 | | |
293 | 304 | | |
294 | | - | |
295 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
296 | 315 | | |
297 | 316 | | |
298 | | - | |
299 | | - | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
300 | 320 | | |
301 | 321 | | |
302 | 322 | | |
| |||
0 commit comments