PkmnLib_rs/src/script_implementations/wasm/script_function_cache.rs

71 lines
2.4 KiB
Rust
Raw Normal View History

2022-08-20 10:22:12 +00:00
use std::sync::Arc;
use parking_lot::RwLock;
use paste::paste;
use crate::dynamic_data::{DynamicLibrary, TurnChoice};
use crate::script_implementations::wasm::extern_ref::{ExternRef, VecExternRef};
use crate::script_implementations::wasm::script_resolver::WebAssemblyEnvironmentData;
use crate::static_data::EffectParameter;
use crate::StringKey;
2022-08-27 12:14:58 +00:00
use wasmer::TypedFunction;
2022-08-20 10:22:12 +00:00
/// A macro to generate the script function cache a bit easier.
2022-08-20 10:22:12 +00:00
macro_rules! script_function_cache {
(
$(
$name:ident($($par_type:ty),*$(,)?)
2022-08-20 10:22:12 +00:00
)*
) => {
#[derive(Default)]
#[allow(unused_parens)]
2022-08-20 10:22:12 +00:00
pub struct ScriptFunctionCache {
$(
2022-08-27 12:14:58 +00:00
$name: RwLock<Option<TypedFunction<(u32$(, $par_type)*), ()>>>,
2022-08-20 10:22:12 +00:00
)*
}
impl ScriptFunctionCache {
$(
paste! {
#[cold]
#[allow(unused_parens)]
2022-08-20 10:22:12 +00:00
fn [<initialize_ $name>](&self, env: &Arc<WebAssemblyEnvironmentData>) {
let exported = env.exported_functions();
let f = exported.get::<StringKey>(&stringify!([< script_ $name >]).into());
if let Some(f) = f {
2022-08-27 12:14:58 +00:00
let func: TypedFunction<(u32 $(,$par_type)*), ()> = f.typed(&env.store_ref()).unwrap();
2022-08-20 10:22:12 +00:00
let _ = self.$name.write().insert(func);
}
}
#[allow(unused_parens)]
2022-08-20 10:22:12 +00:00
pub(crate) fn [<$name>](
&self,
env: &Arc<WebAssemblyEnvironmentData>,
2022-08-27 12:14:58 +00:00
) -> Option<TypedFunction<(u32 $(,$par_type)*), ()>> {
2022-08-20 10:22:12 +00:00
{
let read_lock = self.$name.read();
if let Some(f) = read_lock.as_ref() {
return Some(f.clone());
}
}
self.[<initialize_ $name>](env);
self.$name.read().as_ref().cloned()
}
}
)*
}
}
}
script_function_cache! {
stack()
on_remove()
on_initialize(ExternRef<DynamicLibrary>, VecExternRef<EffectParameter>)
on_before_turn(ExternRef<TurnChoice>)
change_speed(ExternRef<TurnChoice>, u32)
change_priority(ExternRef<TurnChoice>, u32)
2022-08-20 10:22:12 +00:00
}