|
| 1 | +//! Contains abstractions for exposing "globals" to scripts, in a language-agnostic way. |
| 2 | +
|
| 3 | +use super::{ |
| 4 | + function::arg_meta::{ScriptReturn, TypedScriptReturn}, |
| 5 | + script_value::ScriptValue, |
| 6 | + WorldGuard, |
| 7 | +}; |
| 8 | +use crate::{docgen::typed_through::ThroughTypeInfo, error::InteropError}; |
| 9 | +use bevy::{ecs::system::Resource, utils::hashbrown::HashMap}; |
| 10 | +use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; |
| 11 | +use std::{any::TypeId, borrow::Cow, sync::Arc}; |
| 12 | + |
| 13 | +pub mod core; |
| 14 | + |
| 15 | +/// A send + sync wrapper around the [`ScriptGlobalsRegistry`]. |
| 16 | +#[derive(Default, Resource, Clone)] |
| 17 | +pub struct AppScriptGlobalsRegistry(Arc<RwLock<ScriptGlobalsRegistry>>); |
| 18 | + |
| 19 | +impl AppScriptGlobalsRegistry { |
| 20 | + /// Returns a reference to the inner [`ScriptGlobalsRegistry`]. |
| 21 | + pub fn read(&self) -> RwLockReadGuard<ScriptGlobalsRegistry> { |
| 22 | + self.0.read() |
| 23 | + } |
| 24 | + |
| 25 | + /// Returns a mutable reference to the inner [`ScriptGlobalsRegistry`]. |
| 26 | + pub fn write(&self) -> RwLockWriteGuard<ScriptGlobalsRegistry> { |
| 27 | + self.0.write() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// A function that creates a global variable. |
| 32 | +pub type ScriptGlobalMakerFn<T> = |
| 33 | + dyn Fn(WorldGuard) -> Result<T, InteropError> + 'static + Send + Sync; |
| 34 | + |
| 35 | +/// A global variable that can be exposed to scripts. |
| 36 | +pub struct ScriptGlobal { |
| 37 | + /// The function that creates the global variable. |
| 38 | + /// if not present, this is assumed to be a static global, one that |
| 39 | + /// cannot be instantiated, but carries type information. |
| 40 | + pub maker: Option<Arc<ScriptGlobalMakerFn<ScriptValue>>>, |
| 41 | + /// The documentation for the global variable. |
| 42 | + pub documentation: Option<Cow<'static, str>>, |
| 43 | + /// The type ID of the global variable. |
| 44 | + pub type_id: TypeId, |
| 45 | + /// Rich type information the global variable. |
| 46 | + pub type_information: Option<ThroughTypeInfo>, |
| 47 | +} |
| 48 | + |
| 49 | +/// A registry of global variables that can be exposed to scripts. |
| 50 | +#[derive(Default)] |
| 51 | +pub struct ScriptGlobalsRegistry { |
| 52 | + globals: HashMap<Cow<'static, str>, ScriptGlobal>, |
| 53 | +} |
| 54 | + |
| 55 | +impl ScriptGlobalsRegistry { |
| 56 | + /// Gets the global with the given name |
| 57 | + pub fn get(&self, name: &str) -> Option<&ScriptGlobal> { |
| 58 | + self.globals.get(name) |
| 59 | + } |
| 60 | + |
| 61 | + /// Gets the global with the given name mutably |
| 62 | + pub fn get_mut(&mut self, name: &str) -> Option<&mut ScriptGlobal> { |
| 63 | + self.globals.get_mut(name) |
| 64 | + } |
| 65 | + |
| 66 | + /// Counts the number of globals in the registry |
| 67 | + pub fn len(&self) -> usize { |
| 68 | + self.globals.len() |
| 69 | + } |
| 70 | + |
| 71 | + /// Checks if the registry is empty |
| 72 | + pub fn is_empty(&self) -> bool { |
| 73 | + self.len() == 0 |
| 74 | + } |
| 75 | + |
| 76 | + /// Iterates over the globals in the registry |
| 77 | + pub fn iter(&self) -> impl Iterator<Item = (&Cow<'static, str>, &ScriptGlobal)> { |
| 78 | + self.globals.iter() |
| 79 | + } |
| 80 | + |
| 81 | + /// Iterates over the globals in the registry mutably |
| 82 | + pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Cow<'static, str>, &mut ScriptGlobal)> { |
| 83 | + self.globals.iter_mut() |
| 84 | + } |
| 85 | + |
| 86 | + fn type_erase_maker< |
| 87 | + T: ScriptReturn, |
| 88 | + F: Fn(WorldGuard) -> Result<T, InteropError> + Send + Sync + 'static, |
| 89 | + >( |
| 90 | + maker: F, |
| 91 | + ) -> Arc<ScriptGlobalMakerFn<ScriptValue>> { |
| 92 | + Arc::new(move |world| T::into_script(maker(world.clone())?, world)) |
| 93 | + } |
| 94 | + |
| 95 | + /// Inserts a global into the registry, returns the previous value if it existed |
| 96 | + pub fn register< |
| 97 | + T: ScriptReturn + 'static, |
| 98 | + F: Fn(WorldGuard) -> Result<T, InteropError> + 'static + Send + Sync, |
| 99 | + >( |
| 100 | + &mut self, |
| 101 | + name: Cow<'static, str>, |
| 102 | + maker: F, |
| 103 | + ) -> Option<ScriptGlobal> { |
| 104 | + self.globals.insert( |
| 105 | + name, |
| 106 | + ScriptGlobal { |
| 107 | + maker: Some(Self::type_erase_maker(maker)), |
| 108 | + documentation: None, |
| 109 | + type_id: TypeId::of::<T>(), |
| 110 | + type_information: None, |
| 111 | + }, |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + /// Inserts a global into the registry, returns the previous value if it existed. |
| 116 | + /// |
| 117 | + /// This is a version of [`Self::register`] which stores type information regarding the global. |
| 118 | + pub fn register_documented< |
| 119 | + T: TypedScriptReturn + 'static, |
| 120 | + F: Fn(WorldGuard) -> Result<T, InteropError> + 'static + Send + Sync, |
| 121 | + >( |
| 122 | + &mut self, |
| 123 | + name: Cow<'static, str>, |
| 124 | + maker: F, |
| 125 | + documentation: Cow<'static, str>, |
| 126 | + ) -> Option<ScriptGlobal> { |
| 127 | + self.globals.insert( |
| 128 | + name, |
| 129 | + ScriptGlobal { |
| 130 | + maker: Some(Self::type_erase_maker(maker)), |
| 131 | + documentation: Some(documentation), |
| 132 | + type_id: TypeId::of::<T>(), |
| 133 | + type_information: Some(T::through_type_info()), |
| 134 | + }, |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + /// Registers a static global into the registry. |
| 139 | + pub fn register_static<T: 'static>(&mut self, name: Cow<'static, str>) { |
| 140 | + self.globals.insert( |
| 141 | + name, |
| 142 | + ScriptGlobal { |
| 143 | + maker: None, |
| 144 | + documentation: None, |
| 145 | + type_id: TypeId::of::<T>(), |
| 146 | + type_information: None, |
| 147 | + }, |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /// Registers a static global into the registry. |
| 152 | + /// |
| 153 | + /// This is a version of [`Self::register_static`] which stores rich type information regarding the global. |
| 154 | + pub fn register_static_documented<T: TypedScriptReturn + 'static>( |
| 155 | + &mut self, |
| 156 | + name: Cow<'static, str>, |
| 157 | + documentation: Cow<'static, str>, |
| 158 | + ) { |
| 159 | + self.globals.insert( |
| 160 | + name, |
| 161 | + ScriptGlobal { |
| 162 | + maker: None, |
| 163 | + documentation: Some(documentation), |
| 164 | + type_id: TypeId::of::<T>(), |
| 165 | + type_information: Some(T::through_type_info()), |
| 166 | + }, |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /// Registers a static global into the registry. |
| 171 | + /// |
| 172 | + /// This is a version of [`Self::register_static_documented`] which does not require compile time type knowledge. |
| 173 | + pub fn register_static_documented_dynamic( |
| 174 | + &mut self, |
| 175 | + type_id: TypeId, |
| 176 | + type_information: Option<ThroughTypeInfo>, |
| 177 | + name: Cow<'static, str>, |
| 178 | + documentation: Cow<'static, str>, |
| 179 | + ) { |
| 180 | + self.globals.insert( |
| 181 | + name, |
| 182 | + ScriptGlobal { |
| 183 | + maker: None, |
| 184 | + documentation: Some(documentation), |
| 185 | + type_id, |
| 186 | + type_information, |
| 187 | + }, |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +#[cfg(test)] |
| 193 | +mod test { |
| 194 | + use bevy::ecs::world::World; |
| 195 | + |
| 196 | + use super::*; |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn test_script_globals_registry() { |
| 200 | + let mut registry = ScriptGlobalsRegistry::default(); |
| 201 | + |
| 202 | + let maker = |_: WorldGuard| Ok(ScriptValue::from(42)); |
| 203 | + let maker2 = |_: WorldGuard| Ok(ScriptValue::from(43)); |
| 204 | + |
| 205 | + assert_eq!(registry.len(), 0); |
| 206 | + assert!(registry.is_empty()); |
| 207 | + |
| 208 | + assert!(registry.register(Cow::Borrowed("foo"), maker).is_none()); |
| 209 | + assert_eq!(registry.len(), 1); |
| 210 | + |
| 211 | + assert_eq!( |
| 212 | + (registry.get("foo").unwrap().maker.clone().unwrap())(WorldGuard::new( |
| 213 | + &mut World::new() |
| 214 | + )) |
| 215 | + .unwrap(), |
| 216 | + ScriptValue::from(42) |
| 217 | + ); |
| 218 | + |
| 219 | + assert!(registry.register(Cow::Borrowed("foo"), maker2).is_some()); |
| 220 | + assert_eq!(registry.len(), 1); |
| 221 | + |
| 222 | + assert_eq!( |
| 223 | + (registry.get("foo").unwrap().maker.clone().unwrap())(WorldGuard::new( |
| 224 | + &mut World::new() |
| 225 | + )) |
| 226 | + .unwrap(), |
| 227 | + ScriptValue::from(43) |
| 228 | + ); |
| 229 | + } |
| 230 | + |
| 231 | + #[test] |
| 232 | + fn test_documentation_is_stored() { |
| 233 | + let mut registry = ScriptGlobalsRegistry::default(); |
| 234 | + |
| 235 | + let maker = |_: WorldGuard| Ok(ScriptValue::from(42)); |
| 236 | + |
| 237 | + assert!(registry |
| 238 | + .register_documented(Cow::Borrowed("foo"), maker, Cow::Borrowed("This is a test")) |
| 239 | + .is_none()); |
| 240 | + |
| 241 | + let global = registry.get("foo").unwrap(); |
| 242 | + assert_eq!(global.documentation.as_deref(), Some("This is a test")); |
| 243 | + } |
| 244 | + |
| 245 | + #[test] |
| 246 | + fn test_static_globals() { |
| 247 | + let mut registry = ScriptGlobalsRegistry::default(); |
| 248 | + |
| 249 | + registry.register_static::<i32>(Cow::Borrowed("foo")); |
| 250 | + |
| 251 | + let global = registry.get("foo").unwrap(); |
| 252 | + assert!(global.maker.is_none()); |
| 253 | + assert_eq!(global.type_id, TypeId::of::<i32>()); |
| 254 | + |
| 255 | + // the same but documented |
| 256 | + registry.register_static_documented::<i32>( |
| 257 | + Cow::Borrowed("bar"), |
| 258 | + Cow::Borrowed("This is a test"), |
| 259 | + ); |
| 260 | + |
| 261 | + let global = registry.get("bar").unwrap(); |
| 262 | + assert!(global.maker.is_none()); |
| 263 | + assert_eq!(global.type_id, TypeId::of::<i32>()); |
| 264 | + assert_eq!(global.documentation.as_deref(), Some("This is a test")); |
| 265 | + } |
| 266 | +} |
0 commit comments