This commit is contained in:
62
src/script_implementations/wasm/script_function_cache.rs
Normal file
62
src/script_implementations/wasm/script_function_cache.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use parking_lot::RwLock;
|
||||
use paste::paste;
|
||||
use wasmer::NativeFunc;
|
||||
|
||||
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;
|
||||
|
||||
macro_rules! script_function_cache {
|
||||
(
|
||||
$(
|
||||
$name:ident -> $return:ty
|
||||
)*
|
||||
) => {
|
||||
#[derive(Default)]
|
||||
pub struct ScriptFunctionCache {
|
||||
$(
|
||||
$name: RwLock<Option<$return>>,
|
||||
)*
|
||||
}
|
||||
|
||||
impl ScriptFunctionCache {
|
||||
$(
|
||||
paste! {
|
||||
#[cold]
|
||||
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 {
|
||||
let func: $return = f.native().unwrap();
|
||||
let _ = self.$name.write().insert(func);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn [<$name>](
|
||||
&self,
|
||||
env: &Arc<WebAssemblyEnvironmentData>,
|
||||
) -> Option<$return> {
|
||||
{
|
||||
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! {
|
||||
on_initialize -> NativeFunc<(u32, ExternRef<DynamicLibrary>, VecExternRef<EffectParameter>), ()>
|
||||
on_before_turn -> NativeFunc<(u32, u32), ()>
|
||||
}
|
||||
Reference in New Issue
Block a user