Skip to content

Commit

Permalink
Fix MSRV compilation (rustwasm#3927)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed May 21, 2024
1 parent d48af8e commit 00d2d35
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* Fix `catch` not being thread-safe.
[#3879](https://github.com/rustwasm/wasm-bindgen/pull/3879)

* Fix MSRV compilation.
[#3927](https://github.com/rustwasm/wasm-bindgen/pull/3927)

--------------------------------------------------------------------------------

## [0.2.92](https://github.com/rustwasm/wasm-bindgen/compare/0.2.91...0.2.92)
Expand Down
6 changes: 3 additions & 3 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,9 +1658,9 @@ fn splat(
let mut args = Vec::new();
let mut names = Vec::new();

for n in 1..=4 {
let arg_name = format_ident!("{name}_{n}");
let prim_name = format_ident!("Prim{n}");
for n in 1_u32..=4 {
let arg_name = format_ident!("{}_{}", name, n);
let prim_name = format_ident!("Prim{}", n);
args.push(quote! {
#arg_name: <#abi as #wasm_bindgen::convert::WasmAbi>::#prim_name
});
Expand Down
75 changes: 75 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,81 @@ pub mod __rt {
}
}
}

pub const fn flat_len<T, const SIZE: usize>(slices: [&[T]; SIZE]) -> usize {
let mut len = 0;
let mut i = 0;
while i < slices.len() {
len += slices[i].len();
i += 1;
}
len
}

pub const fn flat_byte_slices<const RESULT_LEN: usize, const SIZE: usize>(
slices: [&[u8]; SIZE],
) -> [u8; RESULT_LEN] {
let mut result = [0; RESULT_LEN];

let mut slice_index = 0;
let mut result_offset = 0;

while slice_index < slices.len() {
let mut i = 0;
let slice = slices[slice_index];
while i < slice.len() {
result[result_offset] = slice[i];
i += 1;
result_offset += 1;
}
slice_index += 1;
}

result
}

// NOTE: This method is used to encode u32 into a variable-length-integer during the compile-time .
// Generally speaking, the length of the encoded variable-length-integer depends on the size of the integer
// but the maximum capacity can be used here to simplify the amount of code during the compile-time .
pub const fn encode_u32_to_fixed_len_bytes(value: u32) -> [u8; 5] {
let mut result: [u8; 5] = [0; 5];
let mut i = 0;
while i < 4 {
result[i] = ((value >> (7 * i)) | 0x80) as u8;
i += 1;
}
result[4] = (value >> (7 * 4)) as u8;
result
}

if_std! {
use core::mem;
use std::boxed::Box;

/// Trait for element types to implement `Into<JsValue>` for vectors of
/// themselves, which isn't possible directly thanks to the orphan rule.
pub trait VectorIntoJsValue: Sized {
fn vector_into_jsvalue(vector: Box<[Self]>) -> JsValue;
}

impl<T: VectorIntoJsValue> From<Box<[T]>> for JsValue {
fn from(vector: Box<[T]>) -> Self {
T::vector_into_jsvalue(vector)
}
}

pub fn js_value_vector_into_jsvalue<T: Into<JsValue>>(vector: Box<[T]>) -> JsValue {
let result = unsafe { JsValue::_new(super::__wbindgen_array_new()) };
for value in vector.into_vec() {
let js: JsValue = value.into();
unsafe { super::__wbindgen_array_push(result.idx, js.idx) }
// `__wbindgen_array_push` takes ownership over `js` and has already dropped it,
// so don't drop it again.
mem::forget(js);
}
result
}
}
}

/// A wrapper type around slices and vectors for binding the `Uint8ClampedArray`
Expand Down

0 comments on commit 00d2d35

Please sign in to comment.