Skip to content

Commit

Permalink
improvement(abi): replace the dispatch table's function pointer sette…
Browse files Browse the repository at this point in the history
…rs with mutable getters
  • Loading branch information
Wodann committed Oct 18, 2019
1 parent 739ae2e commit ffcf3e4
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions crates/mun_abi/src/autogen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,22 @@ impl DispatchTable {
}
}

pub unsafe fn set_ptr_unchecked(&mut self, idx: u32, ptr: *const c_void) {
*self.fn_ptrs.offset(idx as isize) = ptr;
}

pub fn set_ptr(&mut self, idx: u32, ptr: *const c_void) -> bool {
/// Returns a mutable reference to a function pointer, without doing bounds checking.
///
/// This is generally not recommended, use with caution! Calling this method with an
/// out-of-bounds index is _undefined behavior_ even if the resulting reference is not used.
/// For a safe alternative see [get_ptr_mut](#method.get_ptr_mut).
pub unsafe fn get_ptr_unchecked_mut(&self, idx: u32) -> &mut *const c_void {
&mut *self.fn_ptrs.offset(idx as isize)
}

/// Returns a mutable reference to a function pointer at the given index, or `None` if out of
/// bounds.
pub fn get_ptr_mut(&self, idx: u32) -> Option<&mut *const c_void> {
if idx < self.num_entries {
unsafe { self.set_ptr_unchecked(idx, ptr) };
true
Some(unsafe { self.get_ptr_unchecked_mut(idx) })
} else {
false
None
}
}
}
Expand Down Expand Up @@ -456,7 +462,7 @@ mod tests {
}

#[test]
fn test_dispatch_table_set_ptr_unchecked() {
fn test_dispatch_table_get_ptr_unchecked_mut() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name);

Expand All @@ -467,16 +473,15 @@ mod tests {
let signatures = &[fn_signature];
let fn_ptrs = &mut [ptr::null()];

let mut dispatch_table = fake_dispatch_table(signatures, fn_ptrs);
assert_eq!(unsafe { dispatch_table.get_ptr_unchecked(0) }, fn_ptrs[0]);

let ptr = 0xffffffff as *const c_void;
unsafe { dispatch_table.set_ptr_unchecked(0, ptr) };
assert_eq!(unsafe { dispatch_table.get_ptr_unchecked(0) }, ptr);
let dispatch_table = fake_dispatch_table(signatures, fn_ptrs);
assert_eq!(
unsafe { dispatch_table.get_ptr_unchecked_mut(0) },
&mut fn_ptrs[0]
);
}

#[test]
fn test_dispatch_table_set_ptr_none() {
fn test_dispatch_table_get_ptr_mut_none() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name);

Expand All @@ -487,15 +492,12 @@ mod tests {
let signatures = &[fn_signature];
let fn_ptrs = &mut [ptr::null()];

let mut dispatch_table = fake_dispatch_table(signatures, fn_ptrs);
assert_eq!(dispatch_table.get_ptr(1), None);

let ptr = 0xffffffff as *const c_void;
assert_eq!(dispatch_table.set_ptr(1, ptr), false);
let dispatch_table = fake_dispatch_table(signatures, fn_ptrs);
assert_eq!(dispatch_table.get_ptr_mut(1), None);
}

#[test]
fn test_dispatch_table_set_ptr_some() {
fn test_dispatch_table_get_ptr_mut_some() {
let type_name = CString::new(FAKE_TYPE_NAME).expect("Invalid fake type name.");
let type_info = fake_type_info(&type_name);

Expand All @@ -506,12 +508,8 @@ mod tests {
let signatures = &[fn_signature];
let fn_ptrs = &mut [ptr::null()];

let mut dispatch_table = fake_dispatch_table(signatures, fn_ptrs);
assert_eq!(dispatch_table.get_ptr(0), Some(fn_ptrs[0]));

let ptr = 0xffffffff as *const c_void;
assert_eq!(dispatch_table.set_ptr(0, ptr), true);
assert_eq!(dispatch_table.get_ptr(0), Some(ptr));
let dispatch_table = fake_dispatch_table(signatures, fn_ptrs);
assert_eq!(dispatch_table.get_ptr_mut(0), Some(&mut fn_ptrs[0]));
}

fn fake_assembly_info(
Expand Down

0 comments on commit ffcf3e4

Please sign in to comment.