pub mod builtins;
use std::{collections::HashMap, sync::Arc};
use wasmer::{FunctionEnvMut, Store};
use wasmer_wasix_types::wasi::Errno;
use crate::{
runtime::task_manager::InlineWaker, syscalls::stderr_write, Runtime, SpawnError, WasiEnv,
};
use super::task::{OwnedTaskStatus, TaskJoinHandle, TaskStatus};
pub trait VirtualCommand
where
Self: std::fmt::Debug,
{
fn name(&self) -> &str;
fn as_any(&self) -> &dyn std::any::Any;
fn exec(
&self,
parent_ctx: &FunctionEnvMut<'_, WasiEnv>,
path: &str,
store: &mut Option<Store>,
config: &mut Option<WasiEnv>,
) -> Result<TaskJoinHandle, SpawnError>;
}
#[derive(Debug, Clone)]
pub struct Commands {
commands: HashMap<String, Arc<dyn VirtualCommand + Send + Sync + 'static>>,
}
impl Commands {
fn new() -> Self {
Self {
commands: HashMap::new(),
}
}
pub fn new_with_builtins(runtime: Arc<dyn Runtime + Send + Sync + 'static>) -> Self {
let mut cmd = Self::new();
let cmd_wasmer = builtins::cmd_wasmer::CmdWasmer::new(runtime.clone());
cmd.register_command(cmd_wasmer);
cmd
}
pub fn register_command<C: VirtualCommand + Send + Sync + 'static>(&mut self, cmd: C) {
let path = format!("/bin/{}", cmd.name());
self.register_command_with_path(cmd, path);
}
pub fn register_command_with_path<C: VirtualCommand + Send + Sync + 'static>(
&mut self,
cmd: C,
path: String,
) {
self.commands.insert(path, Arc::new(cmd));
}
pub fn exists(&self, path: &str) -> bool {
let name = path.to_string();
self.commands.contains_key(&name)
}
pub fn get(&self, path: &str) -> Option<&Arc<dyn VirtualCommand + Send + Sync + 'static>> {
self.commands.get(path)
}
pub fn exec(
&self,
parent_ctx: &FunctionEnvMut<'_, WasiEnv>,
path: &str,
store: &mut Option<Store>,
builder: &mut Option<WasiEnv>,
) -> Result<TaskJoinHandle, SpawnError> {
let path = path.to_string();
if let Some(cmd) = self.commands.get(&path) {
cmd.exec(parent_ctx, path.as_str(), store, builder)
} else {
unsafe {
InlineWaker::block_on(stderr_write(
parent_ctx,
format!("wasm command unknown - {}\r\n", path).as_bytes(),
))
}
.ok();
let res = OwnedTaskStatus::new(TaskStatus::Finished(Ok(Errno::Noent.into())));
Ok(res.handle())
}
}
}