Skip to content

Commit

Permalink
Add sys::conv
Browse files Browse the repository at this point in the history
Add `#[deny(unsafe_op_in_unsafe_fn)]` to `script_instance_info`
Use `sys::conv` bools
Final touches
  • Loading branch information
lilizoey committed Jun 14, 2024
1 parent 3e80962 commit 686b59d
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 232 deletions.
6 changes: 3 additions & 3 deletions godot-core/src/builtin/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ mod custom_callable {
let a: &T = CallableUserdata::inner_from_raw(callable_userdata_a);
let b: &T = CallableUserdata::inner_from_raw(callable_userdata_b);

(a == b) as sys::GDExtensionBool
sys::conv::bool_to_sys(a == b)
}

pub unsafe extern "C" fn rust_callable_to_string_display<T: fmt::Display>(
Expand All @@ -444,7 +444,7 @@ mod custom_callable {
let s = crate::builtin::GString::from(c.to_string());

s.move_into_string_ptr(r_out);
*r_is_valid = true as sys::GDExtensionBool;
*r_is_valid = sys::conv::SYS_TRUE;
}

pub unsafe extern "C" fn rust_callable_to_string_named<F>(
Expand All @@ -455,6 +455,6 @@ mod custom_callable {
let w: &mut FnWrapper<F> = CallableUserdata::inner_from_raw(callable_userdata);

w.name.clone().move_into_string_ptr(r_out);
*r_is_valid = true as sys::GDExtensionBool;
*r_is_valid = sys::conv::SYS_TRUE;
}
}
2 changes: 1 addition & 1 deletion godot-core/src/builtin/string/string_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl From<&'static std::ffi::CStr> for StringName {
sys::interface_fn!(string_name_new_with_latin1_chars)(
ptr,
c_str.as_ptr(),
true as sys::GDExtensionBool, // p_is_static
sys::conv::SYS_TRUE, // p_is_static
)
})
};
Expand Down
68 changes: 36 additions & 32 deletions godot-core/src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod traits;
pub mod error;
pub use class_name::ClassName;
pub use godot_convert::{FromGodot, GodotConvert, ToGodot};
use sys::conv::u32_to_usize;
pub use traits::{ArrayElement, GodotType};

pub(crate) use crate::impl_godot_as_self;
Expand Down Expand Up @@ -312,43 +313,46 @@ impl MethodInfo {
default_arguments,
} = info;

// SAFETY: `name` and `return_value` were created from the appropriate method calls, and have not been freed before this.
unsafe {
let _name = StringName::from_owned_string_sys(name);
PropertyInfo::free_owned_property_sys(return_value);
}
// SAFETY: `name` is a pointer that was returned from `StringName::into_owned_string_sys`, and has not been freed before this.
let _name = unsafe { StringName::from_owned_string_sys(name) };

// SAFETY: `return_value` is a pointer that was returned from `PropertyInfo::into_owned_property_sys`, and has not been freed before
// this.
unsafe { PropertyInfo::free_owned_property_sys(return_value) };

// SAFETY:
// - `from_raw_parts_mut`: `arguments` comes from `as_mut_ptr()` on a mutable slice of length `argument_count`, and no other
// accesses to the pointer happens for the lifetime of the slice.
// - `Box::from_raw`: The slice was returned from a call to `Box::leak`, and we have ownership of the value behind this pointer.
let arguments = unsafe {
let slice = std::slice::from_raw_parts_mut(arguments, u32_to_usize(argument_count));

// SAFETY: These pointers were both created from a call to `as_mut_ptr` on a slice. Additionally these pointer will not be accessed
// again after this function call.
let (arguments_slice, default_arguments_slice) = unsafe {
(
std::slice::from_raw_parts_mut(
arguments,
argument_count
.try_into()
.expect("gdext only supports targets where u32 <= usize"),
),
std::slice::from_raw_parts_mut(
default_arguments,
default_argument_count
.try_into()
.expect("gdext only supports targets where u32 <= usize"),
),
)
Box::from_raw(slice)
};

// SAFETY: We have exclusive ownership of these slices, and they were originally created from a call to `Box::leak`.
let (_arguments, default_arguments) = unsafe {
(
Box::from_raw(arguments_slice),
Box::from_raw(default_arguments_slice),
)
for info in arguments.iter() {
// SAFETY: These infos were originally created from a call to `PropertyInfo::into_owned_property_sys`, and this method
// will not be called again on this pointer.
unsafe { PropertyInfo::free_owned_property_sys(*info) }
}

// SAFETY:
// - `from_raw_parts_mut`: `default_arguments` comes from `as_mut_ptr()` on a mutable slice of length `default_argument_count`, and no
// other accesses to the pointer happens for the lifetime of the slice.
// - `Box::from_raw`: The slice was returned from a call to `Box::leak`, and we have ownership of the value behind this pointer.
let default_arguments = unsafe {
let slice = std::slice::from_raw_parts_mut(
default_arguments,
u32_to_usize(default_argument_count),
);

Box::from_raw(slice)
};

default_arguments.iter().for_each(|ptr| {
// SAFETY: These pointers were originally created from a call to `Variant::into_owner_var_sys`, and this method will not be
for variant in default_arguments.iter() {
// SAFETY: These pointers were originally created from a call to `Variant::into_owned_var_sys`, and this method will not be
// called again on this pointer.
let _variant = unsafe { Variant::from_owned_var_sys(*ptr) };
});
let _variant = unsafe { Variant::from_owned_var_sys(*variant) };
}
}
}
Loading

0 comments on commit 686b59d

Please sign in to comment.