Skip to content

Commit

Permalink
Merge #1673
Browse files Browse the repository at this point in the history
1673: Add conversion logic for boxed_vec, simplify vec creation code r=MarkMcCaskey a=MarkMcCaskey

Fixes the issue mentioned in #1631 (comment) 



Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
bors[bot] and Mark McCaskey authored Oct 3, 2020
2 parents 7175721 + e6c61c7 commit 1de0606
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
53 changes: 41 additions & 12 deletions lib/c-api/src/wasm_c_api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ macro_rules! wasm_declare_vec_inner {
// TODO: actually implement this
[<wasm_ $name _vec_new_uninitialized>](out, 0);
}

#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: *mut [<wasm_ $name _vec_t>]) {
let vec = &mut *ptr;
if !vec.data.is_null() {
Vec::from_raw_parts(vec.data, vec.size, vec.size);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
}
}
}
}
}
Expand Down Expand Up @@ -96,6 +86,17 @@ macro_rules! wasm_declare_vec {
(*out).size = length;
::std::mem::forget(bytes);
}


#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: *mut [<wasm_ $name _vec_t>]) {
let vec = &mut *ptr;
if !vec.data.is_null() {
Vec::from_raw_parts(vec.data, vec.size, vec.size);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
}
}
}

wasm_declare_vec_inner!($name);
Expand All @@ -113,14 +114,31 @@ macro_rules! wasm_declare_boxed_vec {
pub data: *mut *mut [<wasm_ $name _t>],
}

impl<'a> From<Vec<Box<[<wasm_ $name _t>]>>> for [<wasm_ $name _vec_t>] {
fn from(other: Vec<Box<[<wasm_ $name _t>]>>) -> Self {
let boxed_slice: Box<[Box<[<wasm_ $name _t>]>]> = other.into_boxed_slice();
let mut boxed_slice: Box<[*mut [<wasm_ $name _t>]]> = unsafe { ::std::mem::transmute(boxed_slice) };
let size = boxed_slice.len();
let data = boxed_slice.as_mut_ptr();

::std::mem::forget(boxed_slice);
Self {
size,
data,
}
}
}

// TODO: do this properly
impl [<wasm_ $name _vec_t>] {
pub unsafe fn into_slice(&self) -> Option<&[*mut [<wasm_ $name _t>]]>{
pub unsafe fn into_slice(&self) -> Option<&[Box<[<wasm_ $name _t>]>]>{
if self.data.is_null() {
return None;
}

Some(::std::slice::from_raw_parts(self.data, self.size))
let slice: &[*mut [<wasm_ $name _t>]] = ::std::slice::from_raw_parts(self.data, self.size);
let slice: &[Box<[<wasm_ $name _t>]>] = ::std::mem::transmute(slice);
Some(slice)
}
}

Expand All @@ -146,6 +164,17 @@ macro_rules! wasm_declare_boxed_vec {
(*out).size = length;
::std::mem::forget(bytes);
}

#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: *mut [<wasm_ $name _vec_t>]) {
let vec = &mut *ptr;
if !vec.data.is_null() {
let data: Vec<*mut [<wasm_ $name _t>]> = Vec::from_raw_parts(vec.data, vec.size, vec.size);
let data: Vec<Box<[<wasm_ $name _t>]>> = ::std::mem::transmute(data);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
}
}
}

wasm_declare_vec_inner!($name);
Expand Down
33 changes: 8 additions & 25 deletions lib/c-api/src/wasm_c_api/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::types::{
wasm_byte_vec_t, wasm_exporttype_t, wasm_exporttype_vec_t, wasm_importtype_t,
wasm_importtype_vec_t,
};
use std::mem;
use std::ptr::NonNull;
use std::slice;
use std::sync::Arc;
Expand Down Expand Up @@ -38,37 +37,29 @@ pub unsafe extern "C" fn wasm_module_exports(
module: &wasm_module_t,
out: &mut wasm_exporttype_vec_t,
) {
let mut exports = module
let exports = module
.inner
.exports()
.map(Into::into)
.map(Box::new)
.map(Box::into_raw)
.collect::<Vec<*mut wasm_exporttype_t>>();
.collect::<Vec<Box<wasm_exporttype_t>>>();

debug_assert_eq!(exports.len(), exports.capacity());
out.size = exports.len();
out.data = exports.as_mut_ptr();
mem::forget(exports);
*out = exports.into();
}

#[no_mangle]
pub unsafe extern "C" fn wasm_module_imports(
module: &wasm_module_t,
out: &mut wasm_importtype_vec_t,
) {
let mut imports = module
let imports = module
.inner
.imports()
.map(Into::into)
.map(Box::new)
.map(Box::into_raw)
.collect::<Vec<*mut wasm_importtype_t>>();
.collect::<Vec<Box<wasm_importtype_t>>>();

debug_assert_eq!(imports.len(), imports.capacity());
out.size = imports.len();
out.data = imports.as_mut_ptr();
mem::forget(imports);
*out = imports.into();
}

#[no_mangle]
Expand Down Expand Up @@ -102,16 +93,8 @@ pub unsafe extern "C" fn wasm_module_serialize(
out_ptr: &mut wasm_byte_vec_t,
) {
let mut byte_vec = match module.inner.serialize() {
Ok(mut byte_vec) => {
byte_vec.shrink_to_fit();
byte_vec
}
Ok(byte_vec) => byte_vec,
Err(_) => return,
};
// ensure we won't leak memory
// TODO: use `Vec::into_raw_parts` when it becomes stable
debug_assert_eq!(byte_vec.capacity(), byte_vec.len());
out_ptr.size = byte_vec.len();
out_ptr.data = byte_vec.as_mut_ptr();
mem::forget(byte_vec);
*out_ptr = byte_vec.into();
}
4 changes: 2 additions & 2 deletions lib/c-api/src/wasm_c_api/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ unsafe fn wasm_functype_new_inner(
let params: Vec<ValType> = params
.into_slice()?
.iter()
.map(|&ptr| *ptr)
.map(|ptr| **ptr)
.map(Into::into)
.collect::<Vec<_>>();
let results: Vec<ValType> = results
.into_slice()?
.iter()
.map(|&ptr| *ptr)
.map(|ptr| **ptr)
.map(Into::into)
.collect::<Vec<_>>();

Expand Down

0 comments on commit 1de0606

Please sign in to comment.