improve emscripten sockets and networking

This commit is contained in:
Mark McCaskey
2019-06-28 17:14:00 -07:00
parent cb5d1b42ec
commit 4fcb3c4c83
4 changed files with 344 additions and 58 deletions

View File

@ -57,6 +57,18 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
Some(&*cell_ptr)
}
}
#[inline]
pub unsafe fn deref_mut<'a>(self, memory: &'a Memory) -> Option<&'a mut Cell<T>> {
if (self.offset as usize) + mem::size_of::<T>() >= memory.size().bytes().0 {
return None;
}
let cell_ptr = align_pointer(
memory.view::<u8>().as_ptr().add(self.offset as usize) as usize,
mem::align_of::<T>(),
) as *mut Cell<T>;
Some(&mut *cell_ptr)
}
}
impl<T: Copy + ValueType> WasmPtr<T, Array> {
@ -81,6 +93,31 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
Some(cell_ptrs)
}
}
#[inline]
pub unsafe fn deref_mut<'a>(
self,
memory: &'a Memory,
index: u32,
length: u32,
) -> Option<&'a mut [Cell<T>]> {
// gets the size of the item in the array with padding added such that
// for any index, we will always result an aligned memory access
let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>());
let slice_full_len = index as usize + length as usize;
if (self.offset as usize) + (item_size * slice_full_len) >= memory.size().bytes().0 {
return None;
}
let cell_ptr = align_pointer(
memory.view::<u8>().as_ptr().add(self.offset as usize) as usize,
mem::align_of::<T>(),
) as *mut Cell<T>;
let cell_ptrs = &mut std::slice::from_raw_parts_mut(cell_ptr, slice_full_len)
[index as usize..slice_full_len];
Some(cell_ptrs)
}
}
unsafe impl<T: Copy, Ty> WasmExternType for WasmPtr<T, Ty> {