Skip to content

Commit

Permalink
implement a more general sq get for sq handles
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Aug 26, 2024
1 parent af1aa23 commit bec1c0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/high/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub struct SQHandle<H: IsSQObject> {

impl<H: IsSQObject> SQHandle<H> {
/// creates a new [`SQHandle`] by checking if the sqobject has the correct type at runtime
pub fn new(value: SQObject) -> Result<Self, SQObject> {
pub fn try_new(value: SQObject) -> Result<Self, SQObject> {
let ty = value._Type;
if ty == H::OT_TYPE || ty == H::RT_TYPE {
Ok(Self {
Expand Down
16 changes: 13 additions & 3 deletions src/high/squirrel_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl GetFromSquirrelVm for Option<&mut CPlayer> {
}
}

impl GetFromSquirrelVm for SQHandle<SQClosure> {
impl<T: IsSQObject> GetFromSquirrelVm for SQHandle<T> {
fn get_from_sqvm(
sqvm: NonNull<HSquirrelVM>,
sqfunctions: &SquirrelFunctions,
Expand All @@ -377,7 +377,17 @@ impl GetFromSquirrelVm for SQHandle<SQClosure> {
unsafe {
let mut obj = std::mem::MaybeUninit::<SQObject>::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::<T>()
);
}
}
}
}
}
Expand Down Expand Up @@ -468,7 +478,7 @@ impl<T: IntoSquirrelArgs> GetFromSQObject for SquirrelFn<T> {
#[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,
}
Expand Down

0 comments on commit bec1c0c

Please sign in to comment.