Skip to content

Commit

Permalink
improve SQObject related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Apr 27, 2024
1 parent e29d3c2 commit 89bfa3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
22 changes: 20 additions & 2 deletions src/high/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ pub struct SquirrelFn<T: IntoSquirrelArgs> {
}

impl<T: IntoSquirrelArgs> SquirrelFn<T> {
/// creates a new [`SquirrelFn`] using the invariance of [`SQHandle<SQClosure>`]
///
/// # Safety
///
/// doesn't check if the function passed has the correct args and return type
pub const unsafe fn new_unchecked(obj: SQHandle<SQClosure>) -> Self {
Self {
func: obj,
phantom: PhantomData,
}
}

/// calls the underlying squirrel function on the provided sqvm
///
/// # Errors
Expand Down Expand Up @@ -225,6 +237,12 @@ impl<T: IntoSquirrelArgs> SquirrelFn<T> {
}
}

impl<T: IntoSquirrelArgs> AsRef<SQHandle<SQClosure>> for SquirrelFn<T> {
fn as_ref(&self) -> &SQHandle<SQClosure> {
&self.func
}
}

/// Adds a sqfunction to the registration list
///
/// The sqfunction will be registered when its vm is loaded
Expand Down Expand Up @@ -266,7 +284,7 @@ pub fn register_sq_functions(get_info_func: FuncSQFuncInfo) {
/// # use rrplug::{high::squirrel::SQHandle,bindings::squirreldatatypes::SQClosure};
/// #[rrplug::sqfunction(VM="Server")]
/// fn test_call_sq_object_function() -> Result<(),String> {
/// call_sq_function::<()>(sqvm, sq_functions, "someFunction").map_err(|err| err.to_string())?;
/// call_sq_function::<(), _>(sqvm, sq_functions, "someFunction", ()).map_err(|err| err.to_string())?;
///
/// Ok(())
/// }
Expand Down Expand Up @@ -313,7 +331,7 @@ pub fn call_sq_function<R: GetFromSQObject, A: IntoSquirrelArgs>(
/// # use rrplug::{high::squirrel::SQHandle,bindings::squirreldatatypes::SQClosure};
/// #[rrplug::sqfunction(VM="Server")]
/// fn call_sqvm_function(mut func: SQHandle<SQClosure>) -> Result<(),String>{
/// call_sq_object_function::<()>(sqvm, sq_functions, func).map_err(|err| err.to_string())?;
/// call_sq_object_function::<(), _>(sqvm, sq_functions, func, ()).map_err(|err| err.to_string())?;
///
/// Ok(())
/// }
Expand Down
4 changes: 2 additions & 2 deletions src/high/squirrel_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ push_to_sqvm! {
push_sq_float::<f32>;
push_sq_bool::<bool>;
push_sq_vector::<Vector3>;
push_sq_object::<MaybeUninit<SQObject>>;
push_sq_object::<SQObject>;
}

impl<T> PushToSquirrelVm for Vec<T>
Expand Down Expand Up @@ -287,7 +287,7 @@ get_from_sqvm! {
get_sq_float::<f32>;
get_sq_bool::<bool>;
get_sq_vector::<Vector3>;
get_sq_object::<MaybeUninit<SQObject>>;
get_sq_object::<SQObject>;
}

impl<T> GetFromSquirrelVm for Vec<T>
Expand Down
8 changes: 4 additions & 4 deletions src/mid/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ pub fn push_sq_vector(
pub fn push_sq_object(
sqvm: NonNull<HSquirrelVM>,
sqfunctions: &SquirrelFunctions,
mut object: MaybeUninit<SQObject>,
mut object: SQObject,
) {
unsafe { (sqfunctions.sq_pushobject)(sqvm.as_ptr(), object.as_mut_ptr()) };
unsafe { (sqfunctions.sq_pushobject)(sqvm.as_ptr(), &mut object) };
}

/// gets a array of T at a stack pos
Expand Down Expand Up @@ -343,13 +343,13 @@ pub fn get_sq_object(
sqvm: NonNull<HSquirrelVM>,
sqfunctions: &SquirrelFunctions,
stack_pos: i32,
) -> MaybeUninit<SQObject> {
) -> SQObject {
let mut obj: MaybeUninit<SQObject> = MaybeUninit::uninit();
unsafe {
(sqfunctions.sq_getobject)(sqvm.as_ptr(), stack_pos, obj.as_mut_ptr());
};

obj
unsafe { obj.assume_init() }
}

/// gets a function [`SQObject`] from the sqvm
Expand Down

0 comments on commit 89bfa3f

Please sign in to comment.