Skip to content

Commit

Permalink
Add support for number slices of type MaybeUninit<T> (#4316)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Schmidt <msrd0000@gmail.com>
  • Loading branch information
daxpedda and RunDevelopment authored Dec 6, 2024
1 parent f90ebda commit 8fe5bf1
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 104 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* Add `WASM_BINDGEN_TEST_DRIVER_TIMEOUT` environment variable to control the timeout to start and connect to the test driver.
[#4320](https://github.com/rustwasm/wasm-bindgen/pull/4320)

* Add support for number slices of type `MaybeUninit<T>`.
[#4316](https://github.com/rustwasm/wasm-bindgen/pull/4316)

### Changed

* Remove `once_cell/critical-section` requirement for `no_std` with atomics.
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ wasm-bindgen-test = { path = 'crates/test' }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
js-sys = { path = 'crates/js-sys' }
paste = "1"
serde_derive = "1.0"
wasm-bindgen-futures = { path = 'crates/futures' }
wasm-bindgen-test-crate-a = { path = 'tests/crates/a' }
Expand Down
16 changes: 16 additions & 0 deletions crates/cli/tests/reference/echo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export function echo_vec_u32(a: Uint32Array): Uint32Array;
export function echo_vec_i32(a: Int32Array): Int32Array;
export function echo_vec_u64(a: BigUint64Array): BigUint64Array;
export function echo_vec_i64(a: BigInt64Array): BigInt64Array;
export function echo_vec_uninit_u8(a: Uint8Array): Uint8Array;
export function echo_vec_uninit_i8(a: Int8Array): Int8Array;
export function echo_vec_uninit_u16(a: Uint16Array): Uint16Array;
export function echo_vec_uninit_i16(a: Int16Array): Int16Array;
export function echo_vec_uninit_u32(a: Uint32Array): Uint32Array;
export function echo_vec_uninit_i32(a: Int32Array): Int32Array;
export function echo_vec_uninit_u64(a: BigUint64Array): BigUint64Array;
export function echo_vec_uninit_i64(a: BigInt64Array): BigInt64Array;
export function echo_vec_string(a: (string)[]): (string)[];
export function echo_struct(a: Foo): Foo;
export function echo_vec_struct(a: (Foo)[]): (Foo)[];
Expand Down Expand Up @@ -53,6 +61,14 @@ export function echo_option_vec_u32(a?: Uint32Array): Uint32Array | undefined;
export function echo_option_vec_i32(a?: Int32Array): Int32Array | undefined;
export function echo_option_vec_u64(a?: BigUint64Array): BigUint64Array | undefined;
export function echo_option_vec_i64(a?: BigInt64Array): BigInt64Array | undefined;
export function echo_option_vec_uninit_u8(a?: Uint8Array): Uint8Array | undefined;
export function echo_option_vec_uninit_i8(a?: Int8Array): Int8Array | undefined;
export function echo_option_vec_uninit_u16(a?: Uint16Array): Uint16Array | undefined;
export function echo_option_vec_uninit_i16(a?: Int16Array): Int16Array | undefined;
export function echo_option_vec_uninit_u32(a?: Uint32Array): Uint32Array | undefined;
export function echo_option_vec_uninit_i32(a?: Int32Array): Int32Array | undefined;
export function echo_option_vec_uninit_u64(a?: BigUint64Array): BigUint64Array | undefined;
export function echo_option_vec_uninit_i64(a?: BigInt64Array): BigInt64Array | undefined;
export function echo_option_vec_string(a?: (string)[]): (string)[] | undefined;
export function echo_option_struct(a?: Foo): Foo | undefined;
export function echo_option_vec_struct(a?: (Foo)[]): (Foo)[] | undefined;
Expand Down
232 changes: 232 additions & 0 deletions crates/cli/tests/reference/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,110 @@ export function echo_vec_i64(a) {
return v2;
}

/**
* @param {Uint8Array} a
* @returns {Uint8Array}
*/
export function echo_vec_uninit_u8(a) {
const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_u8(ptr0, len0);
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
return v2;
}

/**
* @param {Int8Array} a
* @returns {Int8Array}
*/
export function echo_vec_uninit_i8(a) {
const ptr0 = passArray8ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_i8(ptr0, len0);
var v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
return v2;
}

/**
* @param {Uint16Array} a
* @returns {Uint16Array}
*/
export function echo_vec_uninit_u16(a) {
const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_u16(ptr0, len0);
var v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
return v2;
}

/**
* @param {Int16Array} a
* @returns {Int16Array}
*/
export function echo_vec_uninit_i16(a) {
const ptr0 = passArray16ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_i16(ptr0, len0);
var v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
return v2;
}

/**
* @param {Uint32Array} a
* @returns {Uint32Array}
*/
export function echo_vec_uninit_u32(a) {
const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_u32(ptr0, len0);
var v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
return v2;
}

/**
* @param {Int32Array} a
* @returns {Int32Array}
*/
export function echo_vec_uninit_i32(a) {
const ptr0 = passArray32ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_i32(ptr0, len0);
var v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
return v2;
}

/**
* @param {BigUint64Array} a
* @returns {BigUint64Array}
*/
export function echo_vec_uninit_u64(a) {
const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_u64(ptr0, len0);
var v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
return v2;
}

/**
* @param {BigInt64Array} a
* @returns {BigInt64Array}
*/
export function echo_vec_uninit_i64(a) {
const ptr0 = passArray64ToWasm0(a, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_vec_uninit_i64(ptr0, len0);
var v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
return v2;
}

function addToExternrefTable0(obj) {
const idx = wasm.__externref_table_alloc();
wasm.__wbindgen_export_2.set(idx, obj);
Expand Down Expand Up @@ -912,6 +1016,134 @@ export function echo_option_vec_i64(a) {
return v2;
}

/**
* @param {Uint8Array | undefined} [a]
* @returns {Uint8Array | undefined}
*/
export function echo_option_vec_uninit_u8(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_u8(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
}
return v2;
}

/**
* @param {Int8Array | undefined} [a]
* @returns {Int8Array | undefined}
*/
export function echo_option_vec_uninit_i8(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray8ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_i8(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayI8FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
}
return v2;
}

/**
* @param {Uint16Array | undefined} [a]
* @returns {Uint16Array | undefined}
*/
export function echo_option_vec_uninit_u16(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_u16(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayU16FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
}
return v2;
}

/**
* @param {Int16Array | undefined} [a]
* @returns {Int16Array | undefined}
*/
export function echo_option_vec_uninit_i16(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray16ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_i16(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayI16FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 2, 2);
}
return v2;
}

/**
* @param {Uint32Array | undefined} [a]
* @returns {Uint32Array | undefined}
*/
export function echo_option_vec_uninit_u32(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_u32(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayU32FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
}
return v2;
}

/**
* @param {Int32Array | undefined} [a]
* @returns {Int32Array | undefined}
*/
export function echo_option_vec_uninit_i32(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray32ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_i32(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayI32FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
}
return v2;
}

/**
* @param {BigUint64Array | undefined} [a]
* @returns {BigUint64Array | undefined}
*/
export function echo_option_vec_uninit_u64(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_u64(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayU64FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
}
return v2;
}

/**
* @param {BigInt64Array | undefined} [a]
* @returns {BigInt64Array | undefined}
*/
export function echo_option_vec_uninit_i64(a) {
var ptr0 = isLikeNone(a) ? 0 : passArray64ToWasm0(a, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
const ret = wasm.echo_option_vec_uninit_i64(ptr0, len0);
let v2;
if (ret[0] !== 0) {
v2 = getArrayI64FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
}
return v2;
}

/**
* @param {(string)[] | undefined} [a]
* @returns {(string)[] | undefined}
Expand Down
17 changes: 17 additions & 0 deletions crates/cli/tests/reference/echo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::mem::MaybeUninit;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -42,6 +43,14 @@ echo!(
(echo_vec_i32, Vec<i32>),
(echo_vec_u64, Vec<u64>),
(echo_vec_i64, Vec<i64>),
(echo_vec_uninit_u8, Vec<MaybeUninit<u8>>),
(echo_vec_uninit_i8, Vec<MaybeUninit<i8>>),
(echo_vec_uninit_u16, Vec<MaybeUninit<u16>>),
(echo_vec_uninit_i16, Vec<MaybeUninit<i16>>),
(echo_vec_uninit_u32, Vec<MaybeUninit<u32>>),
(echo_vec_uninit_i32, Vec<MaybeUninit<i32>>),
(echo_vec_uninit_u64, Vec<MaybeUninit<u64>>),
(echo_vec_uninit_i64, Vec<MaybeUninit<i64>>),
(echo_vec_string, Vec<String>),
(echo_struct, Foo),
(echo_vec_struct, Vec<Foo>),
Expand Down Expand Up @@ -70,6 +79,14 @@ echo!(
(echo_option_vec_i32, Option<Vec<i32>>),
(echo_option_vec_u64, Option<Vec<u64>>),
(echo_option_vec_i64, Option<Vec<i64>>),
(echo_option_vec_uninit_u8, Option<Vec<MaybeUninit<u8>>>),
(echo_option_vec_uninit_i8, Option<Vec<MaybeUninit<i8>>>),
(echo_option_vec_uninit_u16, Option<Vec<MaybeUninit<u16>>>),
(echo_option_vec_uninit_i16, Option<Vec<MaybeUninit<i16>>>),
(echo_option_vec_uninit_u32, Option<Vec<MaybeUninit<u32>>>),
(echo_option_vec_uninit_i32, Option<Vec<MaybeUninit<i32>>>),
(echo_option_vec_uninit_u64, Option<Vec<MaybeUninit<u64>>>),
(echo_option_vec_uninit_i64, Option<Vec<MaybeUninit<i64>>>),
(echo_option_vec_string, Option<Vec<String>>),
(echo_option_struct, Option<Foo>),
(echo_option_vec_struct, Option<Vec<Foo>>)
Expand Down
Loading

0 comments on commit 8fe5bf1

Please sign in to comment.