-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f46277
commit ca81458
Showing
6 changed files
with
104 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use super::*; | ||
|
||
/// A callback that can be called from a script. | ||
pub trait ScriptCallback<R> { | ||
/// Returns the number of arguments the callback expects. | ||
fn argument_count(&self) -> usize; | ||
|
||
/// Calls the callback with the given arguments. | ||
fn call(&self, args: &[ScriptValue]) -> Result<ScriptValue, ScriptError>; | ||
} | ||
|
||
/// Implements the `ScriptCallback` trait for functions with up to 5 arguments. | ||
macro_rules! impl_callback { | ||
(@call $len:literal $self:ident $args:ident ) => { | ||
$self() | ||
}; | ||
|
||
(@call $len:literal $self:ident $args:ident $( $arg:ident ),* ) => { | ||
{ | ||
let mut iter = $args.into_iter(); | ||
|
||
$self( | ||
$($arg::from_script_value(iter.next().unwrap()),)* | ||
) | ||
} | ||
}; | ||
|
||
[ $( $len:literal : ( $( $arg:ident, )* ), )* ] => { | ||
$( | ||
impl< | ||
$( $arg, )* | ||
R, | ||
F, | ||
> ScriptCallback<std::marker::PhantomData<( | ||
$( &$arg, )* | ||
&R, | ||
&F, | ||
)>> for F | ||
where | ||
$( $arg: FromScriptValue, )* | ||
R: ToScriptValue, | ||
F: Fn( $( $arg, )* ) -> R, | ||
{ | ||
fn argument_count(&self) -> usize { | ||
$len | ||
} | ||
|
||
fn call(&self, args: &[ScriptValue]) -> Result<ScriptValue, ScriptError> { | ||
if args.len() != $len { | ||
return Err(ScriptError::ExecutionError(format!( | ||
"Invalid argument count: Expected {}, got {}", | ||
self.argument_count(), | ||
args.len() | ||
))); | ||
} | ||
|
||
// recursive | ||
let result = impl_callback!(@call $len self args $($arg),* ); | ||
|
||
Ok(result.to_script_value()) | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
impl_callback![ | ||
0: (), | ||
1: (A1,), | ||
2: (A1, A2,), | ||
3: (A1, A2, A3,), | ||
4: (A1, A2, A3, A4,), | ||
5: (A1, A2, A3, A4, A5,), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters