use std::any::Any; use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicUsize}; use hashbrown::HashSet; use crate::dynamic_data::Script; use crate::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver; use crate::script_implementations::wasm::WebAssemblyScriptCapabilities; use crate::StringKey; /// A WebAssemblyScript is there to implement the Script trait within WebAssemblyScript. pub struct WebAssemblyScript { /// The unique identifier of the script. name: StringKey, /// Returns an atomic bool for internal marking of deletion. This is currently only specifically /// used for deletion of a script while we are holding a reference to it (i.e. executing a script /// hook on it). marked_for_deletion: AtomicBool, /// A script can be suppressed by other scripts. If a script is suppressed by at least one script /// we will not execute its methods. This holds the number of suppressions on the script. suppressed_count: AtomicUsize, /// The owner of this script (where the script is attached to) owner_ptr: AtomicPtr, /// Pointer inside WebAssembly memory where the data is for this script. self_ptr: u32, /// Capabilities define which functions we actually implement. capabilities: AtomicPtr>, /// A reference back to our resolver. resolver: AtomicPtr, } impl WebAssemblyScript { /// Instantiates a new WebAssemblyScript. pub fn new( owner_ptr: *mut u8, self_ptr: u32, capabilities: *mut HashSet, resolver: *mut WebAssemblyScriptResolver, name: StringKey, ) -> Self { Self { name, marked_for_deletion: Default::default(), suppressed_count: Default::default(), owner_ptr: AtomicPtr::new(owner_ptr), self_ptr, capabilities: AtomicPtr::new(capabilities), resolver: AtomicPtr::new(resolver), } } } impl Script for WebAssemblyScript { fn name(&self) -> &StringKey { &self.name } fn get_marked_for_deletion(&self) -> &AtomicBool { &self.marked_for_deletion } fn get_suppressed_count(&self) -> &AtomicUsize { &self.suppressed_count } fn as_any(&self) -> &dyn Any { self } fn as_any_mut(&mut self) -> &mut dyn Any { self } }