From bec1c0c9a88b9bccdce73ea8422e3e38c0544494 Mon Sep 17 00:00:00 2001 From: cat_or_not <41955154+catornot@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:19:58 -0400 Subject: [PATCH] implement a more general sq get for sq handles --- src/high/squirrel.rs | 2 +- src/high/squirrel_traits.rs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/high/squirrel.rs b/src/high/squirrel.rs index f47ce95..7b4d021 100644 --- a/src/high/squirrel.rs +++ b/src/high/squirrel.rs @@ -140,7 +140,7 @@ pub struct SQHandle { impl SQHandle { /// creates a new [`SQHandle`] by checking if the sqobject has the correct type at runtime - pub fn new(value: SQObject) -> Result { + pub fn try_new(value: SQObject) -> Result { let ty = value._Type; if ty == H::OT_TYPE || ty == H::RT_TYPE { Ok(Self { diff --git a/src/high/squirrel_traits.rs b/src/high/squirrel_traits.rs index 496aab5..ee65944 100644 --- a/src/high/squirrel_traits.rs +++ b/src/high/squirrel_traits.rs @@ -368,7 +368,7 @@ impl GetFromSquirrelVm for Option<&mut CPlayer> { } } -impl GetFromSquirrelVm for SQHandle { +impl GetFromSquirrelVm for SQHandle { fn get_from_sqvm( sqvm: NonNull, sqfunctions: &SquirrelFunctions, @@ -377,7 +377,17 @@ impl GetFromSquirrelVm for SQHandle { unsafe { let mut obj = std::mem::MaybeUninit::::uninit(); (sqfunctions.sq_getobject)(sqvm.as_ptr(), stack_pos, obj.as_mut_ptr()); - Self::new(obj.assume_init()).expect("the SQObject wasn't a closure") + + match Self::try_new(obj.assume_init()) { + Ok(handle) => handle, + Err(_) => { + panic!( + "the object wasn't the correct type got {:X} expected {}", + obj.assume_init()._Type as i32, + std::any::type_name::() + ); + } + } } } } @@ -468,7 +478,7 @@ impl GetFromSQObject for SquirrelFn { #[inline] fn get_from_sqobject(obj: &SQObject) -> Self { SquirrelFn { - func: SQHandle::new(obj.to_owned()) + func: SQHandle::try_new(obj.to_owned()) .expect("the squirrel object wasn't a function lol L"), phantom: std::marker::PhantomData, }