Skip to main content

uni_plugin_rhai/
runtime.rs

1//! Shared per-plugin runtime — the `Engine` + `AST` adapters hold by
2//! reference.
3//!
4//! Each loaded Rhai plugin owns one [`RhaiPluginRuntime`]: a Send + Sync
5//! handle wrapping the configured `rhai::Engine` and the compiled `AST`.
6//! Adapters (`RhaiScalarFn`, `RhaiAggregateFn`, `RhaiProcedure`) hold
7//! `Arc<RhaiPluginRuntime>` clones so the underlying state isn't
8//! duplicated per registered function.
9
10#![cfg(feature = "rhai-runtime")]
11
12use std::sync::Arc;
13
14use rhai::{AST, Engine};
15
16use uni_plugin::PluginId;
17
18/// Per-plugin runtime state shared across every adapter the plugin
19/// registers.
20#[derive(Debug)]
21pub struct RhaiPluginRuntime {
22    /// The plugin's id (for error reporting).
23    pub plugin_id: PluginId,
24    /// The Rhai engine, configured for this plugin's effective
25    /// capability set. `Send + Sync` via Rhai's `sync` feature.
26    pub engine: Arc<Engine>,
27    /// The compiled script AST. `Send + Sync` and cheaply `Clone`.
28    pub ast: Arc<AST>,
29}
30
31impl RhaiPluginRuntime {
32    /// Construct a runtime handle.
33    #[must_use]
34    pub fn new(plugin_id: PluginId, engine: Engine, ast: AST) -> Arc<Self> {
35        Arc::new(Self {
36            plugin_id,
37            engine: Arc::new(engine),
38            ast: Arc::new(ast),
39        })
40    }
41}