Add slice_to_array attribute for plain-Array slice imports#5145
Merged
Conversation
fb07f1b to
5709768
Compare
hoodmane
reviewed
May 6, 2026
hoodmane
reviewed
May 6, 2026
Adds an opt-in per-fn or per-`extern "C"`-block attribute that lets
idiomatic `&[T]` arguments be used when binding JS APIs that take a
plain `Array<T>` rather than a typed array.
Idiomatic Rust uses `&[T]` for borrowed sequences. When binding a JS
function that takes an array of values, the natural Rust signature is
`fn foo(items: &[T])`. The default `&[T]` binding is split by element
kind today: primitive `T` arrives as a typed-array view (`Uint32Array`
etc.) because of a zero-copy buffer optimisation, while `String` and
JS-imported types arrive as a plain `Array`. There's no way to ask
for the plain-`Array` form for primitive `T` without abandoning
`&[T]` for `Vec<JsValue>` or hand-rolled plumbing.
`slice_to_array` makes `&[T]` (and `Option<&[T]>`) arrive as a plain
`Array` regardless of element kind, while keeping the user-facing
`&[T]` signature unchanged:
#[wasm_bindgen]
extern "C" {
// JS receives Array<number>, signature stays idiomatic.
#[wasm_bindgen(slice_to_array)]
fn set_indices(values: &[u16]);
}
Block-form for module bindings with a consistent convention:
#[wasm_bindgen(module = "/lib.js", slice_to_array)]
extern "C" {
fn take_numbers(v: &[i32]);
fn take_strings(v: &[String]);
fn take_optional(v: Option<&[u16]>);
}
Implementation:
* `slice_to_array` parsed in the macro at fn-arg, fn, and extern block
levels; purely additive opt-in.
* New internal `VectorRefIntoWasmAbi` trait dispatches the per-element
ABI conversion. Two impl shapes, neither requiring `T: Clone`:
- Primitive `T`: zero-copy borrow of the slice memory (no
allocation, same wire as plain `&[T]`). The JS-side shim does
`Array.from(typedArrayView)` and never frees the buffer.
- Blanket `for<'a> &'a T: Into<JsValue>` (covers `String`,
`JsValue`, and JS-imported types): iterate the slice and build a
fresh `[u32]` index buffer, one externref per element. JS reads
the indices into a plain `Array` and frees the index buffer.
* New CLI instructions `VectorLoadAsArray` /
`OptionVectorLoadAsArray` bind the resulting descriptor; the JS
shim picks the right helper (and decides whether to free) based on
the element `VectorKind`.
* `&[ExportedRustStruct]` remains unsupported — the existing default
`&[T]` doesn't support exported structs either, and a borrow form
for them would require either deep cloning the data per element or
reworking the exported-struct ownership model. Owned `Vec<T>`
continues to work for that case.
Has no effect on exported functions; default `&[T]` (typed-array view
/ zero-copy memory borrow) and owned `Vec<T>` semantics are unchanged
for callers that didn't opt in.
Test coverage:
* wasm runtime tests in `tests/wasm/slice_to_array.{rs,js}` covering
`&[u8|u16|i32|f64]`, `&[String]`, `&[ImportedType]`,
`Option<&[u16]>`, plus block-level fan-out.
* reference test in `crates/cli/tests/reference/slice-to-array.*`
locking in the bg.js / wat output, including the no-free codegen
for primitive borrows.
* new guide page at
`reference/attributes/on-js-imports/slice_to_array.html`.
4d6ef98 to
d8d4ad2
Compare
This was referenced May 6, 2026
Merged
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.
This adds the
slice_to_arrayattribute for imported JS functions, an opt-in per-fn or per-extern "C"-block that lets idiomatic&[T]arguments be used when binding JS APIs that take a plainArray<T>rather than a typed array.Idiomatic Rust uses
&[T]for borrowed sequences. When binding a JS function that takes an array of values, the natural Rust signature isfn foo(items: &[T]). The default&[T]binding is split by element kind today: primitiveTarrives as a typed-array view (Uint32Arrayetc.) because of a zero-copy buffer optimisation, whileStringand JS-imported types arrive as a plainArray. There's no way to ask for the plain-Arrayform for primitiveTwithout abandoning&[T]forVec<JsValue>or hand-rolled plumbing.slice_to_arraymakes&[T](andOption<&[T]>) arrive as a plainArrayregardless of element kind, while keeping the user-facing&[T]signature unchanged:Block-form for module bindings with a consistent convention:
Implementation:
slice_to_arrayparsed in the macro at fn-arg, fn, and extern block levels; purely additive opt-in.VectorRefIntoWasmAbitrait dispatches the per-element ABI conversion. Two impl shapes, neither requiringT: Clone:T: zero-copy borrow of the slice memory (no allocation, same wire as plain&[T]). The JS-side shim doesArray.from(typedArrayView)and never frees the buffer.for<'a> &'a T: Into<JsValue>(coversString,JsValue, and JS-imported types): iterate the slice and build a fresh[u32]index buffer, one externref per element. JS reads the indices into a plainArrayand frees the index buffer.VectorLoadAsArray/OptionVectorLoadAsArraybind the resulting descriptor; the JS shim picks the right helper (and decides whether to free) based on the elementVectorKind.&[ExportedRustStruct]remains unsupported — the existing default&[T]doesn't support exported structs either, and a borrow form for them would require either deep cloning the data per element or reworking the exported-struct ownership model. OwnedVec<T>continues to work for that case.Has no effect on exported functions; default
&[T](typed-array view / zero-copy memory borrow) and ownedVec<T>semantics are unchanged for callers that didn't opt in.Test coverage:
tests/wasm/slice_to_array.{rs,js}covering&[u8|u16|i32|f64],&[String],&[ImportedType],Option<&[u16]>, plus block-level fan-out.crates/cli/tests/reference/slice-to-array.*locking in the bg.js / wat output, including the no-free codegen for primitive borrows.reference/attributes/on-js-imports/slice_to_array.html.