Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/libcruby-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ extern "C" {
pub fn rb_define_module_under(namespace: VALUE, name: c_string) -> VALUE;
pub fn rb_define_class(name: c_string, superclass: VALUE) -> VALUE;
pub fn rb_define_class_under(namespace: VALUE, name: c_string, superclass: VALUE) -> VALUE;
pub fn rb_define_alloc_func(klass: VALUE, func: extern "C" fn(klass: VALUE) -> VALUE);
pub fn rb_define_alloc_func(klass: VALUE, func: Option<extern "C" fn(klass: VALUE) -> VALUE>);
pub fn rb_define_method(class: VALUE, name: c_string, func: c_func, arity: isize);
pub fn rb_define_singleton_method(class: VALUE, name: c_string, func: c_func, arity: isize);
pub fn rb_sprintf(specifier: c_string, ...) -> VALUE;
Expand All @@ -200,17 +200,17 @@ extern "C" {
pub fn rb_ary_push(ary: VALUE, item: VALUE) -> VALUE;
pub fn rb_hash_new() -> VALUE;
pub fn rb_hash_aset(hash: VALUE, key: VALUE, value: VALUE) -> VALUE;
pub fn rb_hash_foreach(hash: VALUE, f: extern "C" fn(key: VALUE, value: VALUE, farg: *mut void) -> st_retval, farg: *mut void);
pub fn rb_hash_foreach(hash: VALUE, f: Option<extern "C" fn(key: VALUE, value: VALUE, farg: *mut void) -> st_retval>, farg: *mut void);

pub fn rb_raise(exc: VALUE, string: c_string, ...) -> !;
pub fn rb_jump_tag(state: RubyException) -> !;
pub fn rb_protect(try: extern "C" fn(v: *mut void) -> VALUE,
pub fn rb_protect(try: Option<extern "C" fn(v: *mut void) -> VALUE>,
arg: *mut void,
state: *mut RubyException)
-> VALUE;

#[link_name = "HELIX_Data_Wrap_Struct"]
pub fn Data_Wrap_Struct(klass: VALUE, mark: extern "C" fn(*mut void), free: extern "C" fn(*mut void), data: *mut void) -> VALUE;
pub fn Data_Wrap_Struct(klass: VALUE, mark: Option<extern "C" fn(*mut void)>, free: Option<extern "C" fn(*mut void)>, data: *mut void) -> VALUE;

#[link_name = "HELIX_Data_Get_Struct_Value"]
pub fn Data_Get_Struct_Value(obj: VALUE) -> *mut void;
Expand Down
2 changes: 1 addition & 1 deletion src/class_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ClassDefinition {

pub fn wrapped(name: c_string, alloc_func: extern "C" fn(klass: sys::VALUE) -> sys::VALUE) -> ClassDefinition {
let raw_class = unsafe { sys::rb_define_class(name, sys::rb_cObject) };
unsafe { sys::rb_define_alloc_func(raw_class, alloc_func) };
unsafe { sys::rb_define_alloc_func(raw_class, Some(alloc_func)) };
ClassDefinition { class: Class(raw_class) }
}

Expand Down
2 changes: 1 addition & 1 deletion src/coercions/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<K: FromRuby + Eq + Hash, V: FromRuby> FromRuby for HashMap<K, V> {
let len = unsafe { RHASH_SIZE(value) };

let mut pairs = Vec::<(VALUE, VALUE)>::with_capacity(len as usize);
unsafe { rb_hash_foreach(value, rb_hash_collect, transmute(&mut pairs)) };
unsafe { rb_hash_foreach(value, Some(rb_hash_collect), transmute(&mut pairs)) };

let mut checked = Vec::<(K::Checked, V::Checked)>::with_capacity(len as usize);

Expand Down
24 changes: 15 additions & 9 deletions src/macros/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ macro_rules! codegen_allocator {
methods: [ $($method:tt)* ]
}) => (
impl $rust_name {
extern "C" fn __mark__(_klass: &$rust_name) {}
extern "C" fn __free__(_klass: Option<Box<$rust_name>>) {}
extern "C" fn __free__(this: *mut $crate::libc::c_void) {
if !this.is_null() {
let _ = unsafe { Box::from_raw(this as *mut Self) };
}
}

#[inline]
fn __alloc_with__(rust_self: Option<Box<$rust_name>>) -> $crate::sys::VALUE {
use ::std::mem::transmute;
use ::std::ptr::null_mut;

let ptr = rust_self
.map(Box::into_raw)
.unwrap_or_else(null_mut);

unsafe {
let instance = $crate::sys::Data_Wrap_Struct(
$crate::sys::Data_Wrap_Struct(
transmute($rust_name),
transmute($rust_name::__mark__ as usize),
transmute($rust_name::__free__ as usize),
transmute(rust_self)
);

instance
None,
Some($rust_name::__free__),
ptr as *mut $crate::libc::c_void,
)
}
}
}
Expand Down