//! Runs each script plugin activation in its own confined psychoscript VM on the UI thread. pub(crate) mod bind; pub(crate) mod value; pub(crate) mod vm; #[cfg(test)] mod measure; #[cfg(test)] mod tests; use std::path::Path; use std::rc::Rc; pub use vm::MEMORY; use crate::loader::approval::Artifact; pub(crate) use crate::source::Builds; pub use crate::source::{MANIFEST, Manifest, SOURCES, Sources}; use crate::{Definition, ErrorFn}; /// Why a script plugin is listed and not active while its files are walked off the UI thread. pub const STARTING: &str = "reading its script files"; const TAG: &str = "script:"; /// Tags a directory approval artifact so script and TypeScript approvals stay distinct. pub fn artifact(tree: Artifact) -> Artifact { Artifact { sha256: format!("{TAG}{}", tree.sha256), ..tree } } /// Strips the script approval tag to recover the underlying directory artifact. pub fn tree_of(artifact: &Artifact) -> Artifact { let sha256 = artifact.sha256.strip_prefix(TAG).unwrap_or(&artifact.sha256).to_owned(); Artifact { sha256, ..artifact.clone() } } /// Whether `artifact` is a script plugin's approval artifact. pub fn approves(artifact: &Artifact) -> bool { artifact.sha256.starts_with(TAG) } /// Script plugins are on unless `OPENCODE_PLUGIN_SCRIPT` is `0`, `false` or `off`. pub fn enabled() -> bool { !crate::turned_off(std::env::var_os("OPENCODE_PLUGIN_SCRIPT").as_deref()) } /// Reads `plugin.toml` and every `.ts` (excluding `.d.ts`) and `.js` file in `dir`. pub fn read(dir: &Path) -> Result { Sources::read(dir) } /// Parses `plugin.toml`, defaulting `entry` to `init.ts` (or `init.js` when only `init.js` is present). pub fn parse_manifest(sources: &Sources) -> Result { Manifest::parse(sources) } /// A compiled script plugin, shareable across threads. pub struct Built { manifest: Manifest, compiled: vm::Compiled, } impl Built { pub fn new(sources: &Sources) -> Result { let manifest = parse_manifest(sources)?; let compiled = vm::Compiled::new(sources, &manifest.id, &manifest.entry, bind::PRELUDE)?; Ok(Self { manifest, compiled }) } /// Creates a plugin definition that starts a fresh VM per activation with `options` (JSON) and heap cap `memory`. pub fn define(self, options: Option<&str>, memory: usize) -> Result<(Definition, ErrorFn), String> { let compiled = Rc::new(self.compiled); let options = options.map(str::to_owned); let run: crate::LinkedSetup = Rc::new(move |setup, limits| { bind::setup(&compiled, setup, limits.overrun, memory, options.as_deref()).map(drop) }); let definition = crate::linked(self.manifest.id, run, true); let error = definition.error.clone().ok_or("a linked definition reports its failure")?; Ok((definition, error)) } } pub fn define(sources: &Sources, options: Option<&str>) -> Result<(Definition, ErrorFn), String> { define_with(sources, options, MEMORY) } pub fn define_with(sources: &Sources, options: Option<&str>, memory: usize) -> Result<(Definition, ErrorFn), String> { Built::new(sources)?.define(options, memory) } /// Live reachable heap bytes an idle activation of `sources` holds once setup ran. pub fn idle_memory(sources: &Sources) -> Result { let built = Built::new(sources)?; let mut registered = opencode_plugin_abi::Registered::new(&built.manifest.id, None); let limit = crate::contain::Limits::default().overrun; let plugin = bind::setup(&built.compiled, &mut registered.setup(), limit, MEMORY, None)?; let vm = plugin.vm.borrow(); let vm = vm.as_ref().ok_or("the plugin has ended")?; vm.heap_bytes().ok_or_else(|| "the activation did not measure its heap".to_owned()) }