Skip to content

Commit

Permalink
fix sqvm function crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Mar 19, 2024
1 parent b9c2d35 commit c031667
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/high/engine_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ pub unsafe fn run_async_routine() {
log::warn!("async squirrel function failed to execute; it may not be global");
} else {
unsafe {
let amount = args(sqvm, sqfunctions);

(sqfunctions.sq_pushobject)(sqvm, function_obj.as_mut_ptr());
(sqfunctions.sq_pushroottable)(sqvm);

if (sqfunctions.sq_call)(sqvm, amount, true as u32, true as u32)
let amount = args(sqvm, sqfunctions);

if (sqfunctions.sq_call)(sqvm, amount + 1, true as u32, true as u32)
== SQRESULT::SQRESULT_ERROR
{
log::warn!("async squirrel function failed to execute!")
Expand Down
15 changes: 10 additions & 5 deletions src/high/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use parking_lot::Mutex;
use std::marker::PhantomData;

use super::{
squirrel_traits::{GetFromSquirrelVm, IntoSquirrelArgs, IsSQObject},
squirrel_traits::{GetFromSQObject, IntoSquirrelArgs, IsSQObject},
UnsafeHandle,
};
use crate::{
Expand Down Expand Up @@ -263,7 +263,7 @@ pub fn register_sq_functions(get_info_func: FuncSQFuncInfo) {
/// Ok(())
/// }
/// ```
pub fn call_sq_function<R: GetFromSquirrelVm>(
pub fn call_sq_function<R: GetFromSQObject>(
sqvm: *mut HSquirrelVM,
sqfunctions: &'static SquirrelFunctions,
function_name: impl AsRef<str>,
Expand Down Expand Up @@ -309,7 +309,7 @@ pub fn call_sq_function<R: GetFromSquirrelVm>(
/// Ok(())
/// }
/// ```
pub fn call_sq_object_function<R: GetFromSquirrelVm>(
pub fn call_sq_object_function<R: GetFromSQObject>(
sqvm: *mut HSquirrelVM,
sqfunctions: &'static SquirrelFunctions,
mut obj: SQHandle<SQClosure>,
Expand All @@ -318,7 +318,7 @@ pub fn call_sq_object_function<R: GetFromSquirrelVm>(
}

#[inline]
fn _call_sq_object_function<R: GetFromSquirrelVm>(
fn _call_sq_object_function<R: GetFromSQObject>(
sqvm: *mut HSquirrelVM,
sqfunctions: &'static SquirrelFunctions,
ptr: *mut SQObject,
Expand All @@ -331,7 +331,12 @@ fn _call_sq_object_function<R: GetFromSquirrelVm>(
if (sqfunctions.sq_call)(sqvm, 1, true as u32, true as u32) == SQRESULT::SQRESULT_ERROR {
Err(CallError::FunctionFailedToExecute)
} else {
Ok(R::get_from_sqvm(sqvm, sqfunctions, sqvm._top))
Ok(R::get_from_sqobject(
sqvm._stack
.add(sqvm._top as usize - 1)
.as_ref()
.ok_or(CallError::FunctionFailedToExecute)?,
))
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/high/squirrel_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ pub trait GetFromSQObject {
fn get_from_sqobject(obj: &SQObject) -> Self;
}

impl GetFromSQObject for () {
#[inline]
fn get_from_sqobject(_: &SQObject) -> Self {
()
}
}

impl GetFromSQObject for String {
#[inline]
fn get_from_sqobject(obj: &SQObject) -> Self {
Expand Down

0 comments on commit c031667

Please sign in to comment.