Minimize unsafe block to unsafe code

This commit is contained in:
Brandon Fish
2019-08-10 17:20:27 -06:00
parent b7970fb982
commit 38a8a0eb01

View File

@ -159,8 +159,7 @@ impl LocalBacking {
LocalOrImport::Import(imported_memory_index) => { LocalOrImport::Import(imported_memory_index) => {
// Write the initialization data to the memory that // Write the initialization data to the memory that
// we think the imported memory is. // we think the imported memory is.
unsafe { let local_memory = unsafe { &*imports.vm_memories[imported_memory_index] };
let local_memory = &*imports.vm_memories[imported_memory_index];
let data_top = init_base + init.data.len(); let data_top = init_base + init.data.len();
if local_memory.bound < data_top || data_top < init_base { if local_memory.bound < data_top || data_top < init_base {
return Err(vec![LinkError::Generic { return Err(vec![LinkError::Generic {
@ -170,7 +169,6 @@ impl LocalBacking {
} }
} }
} }
}
// Initialize data // Initialize data
for init in module.info.data_initializers.iter() { for init in module.info.data_initializers.iter() {
@ -199,18 +197,16 @@ impl LocalBacking {
LocalOrImport::Import(imported_memory_index) => { LocalOrImport::Import(imported_memory_index) => {
// Write the initialization data to the memory that // Write the initialization data to the memory that
// we think the imported memory is. // we think the imported memory is.
unsafe { let memory_slice = unsafe {
let local_memory = &*imports.vm_memories[imported_memory_index]; let local_memory = &*imports.vm_memories[imported_memory_index];
let memory_slice = slice::from_raw_parts_mut(local_memory.base, local_memory.bound)
slice::from_raw_parts_mut(local_memory.base, local_memory.bound); };
let mem_init_view = let mem_init_view = &mut memory_slice[init_base..init_base + init.data.len()];
&mut memory_slice[init_base..init_base + init.data.len()];
mem_init_view.copy_from_slice(&init.data); mem_init_view.copy_from_slice(&init.data);
} }
} }
} }
}
Ok(memories Ok(memories
.iter_mut() .iter_mut()