1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
//! Defines plugin related structs.
use crate::{instance::Instance, WasmEdgeResult};
use wasmedge_sys as sys;
pub mod ffi {
pub use wasmedge_sys::ffi::{
WasmEdge_ModuleDescriptor, WasmEdge_ModuleInstanceContext, WasmEdge_PluginDescriptor,
};
}
/// Defines the API to manage plugins.
#[derive(Debug)]
pub struct PluginManager {}
impl PluginManager {
/// Load plugins from the given path.
///
/// * If the path is not given, then the default plugin paths will be used. The default plugin paths are
///
/// * The environment variable "WASMEDGE_PLUGIN_PATH".
///
/// * The `../plugin/` directory related to the WasmEdge installation path.
///
/// * The `wasmedge/` directory under the library path if the WasmEdge is installed under the "/usr".
///
/// * If the path is given, then
///
/// * If the path is pointing at a file , then it indicates that a single plugin will be loaded from the file.
///
/// * If the path is pointing at a directory, then the method will load plugins from the files in the directory.
///
/// # Argument
///
/// * `path` - A path to a plugin file or a directory holding plugin files. If `None`, then the default plugin path will be used.
pub fn load(path: Option<&std::path::Path>) -> WasmEdgeResult<()> {
match path {
Some(p) => sys::plugin::PluginManager::load_plugins(p),
None => {
sys::plugin::PluginManager::load_plugins_from_default_paths();
Ok(())
}
}
}
/// Returns the count of loaded plugins.
pub fn count() -> u32 {
sys::plugin::PluginManager::count()
}
/// Returns the names of all loaded plugins.
pub fn names() -> Vec<String> {
sys::plugin::PluginManager::names()
}
/// Returns the target plugin by its name.
///
/// # Argument
///
/// * `name` - The name of the target plugin.
pub fn find(name: impl AsRef<str>) -> Option<Plugin> {
sys::plugin::PluginManager::find(name.as_ref()).map(|p| Plugin { inner: p })
}
/// Initializes the `wasmedge_process` plugin module instance with the parameters.
///
/// # Arguments
///
/// * `allowed_cmds` - A white list of commands.
///
/// * `allowed` - Determines if wasmedge_process is allowed to execute all commands on the white list.
#[cfg(all(
target_os = "linux",
feature = "wasmedge_process",
not(feature = "static")
))]
pub fn init_wasmedge_process(allowed_cmds: Option<Vec<&str>>, allowed: bool) {
sys::plugin::PluginManager::init_wasmedge_process(allowed_cmds, allowed);
}
}
/// Represents a loaded plugin. It provides the APIs for accessing the plugin.
#[derive(Debug)]
pub struct Plugin {
inner: sys::plugin::Plugin,
}
impl Plugin {
/// Returns the name of this plugin.
pub fn name(&self) -> String {
self.inner.name()
}
/// Returns the count of the module instances in this plugin.
pub fn mod_count(&self) -> u32 {
self.inner.mod_count()
}
/// Returns the names of all module instances in this plugin.
pub fn mod_names(&self) -> Vec<String> {
self.inner.mod_names()
}
/// Returns a module instance that is generated from the module with the given name in this plugin.
///
/// # Argument
///
/// * `name` - The name of the target module.
pub fn mod_instance(&self, name: impl AsRef<str>) -> Option<Instance> {
self.inner
.mod_instance(name.as_ref())
.map(|i| Instance { inner: i })
}
}
/// Defines the type of the function that creates a module instance for a plugin.
pub type ModuleInstanceCreateFn = sys::plugin::ModuleInstanceCreateFn;
/// Defines the type of the program options.
pub type ProgramOptionType = sys::plugin::ProgramOptionType;
/// Represents Plugin descriptor for plugins.
#[derive(Debug)]
pub struct PluginDescriptor {
inner: sys::plugin::PluginDescriptor,
}
impl PluginDescriptor {
/// Creates a new plugin descriptor.
///
/// # Arguments
///
/// * `name` - The name of the plugin.
///
/// * `desc` - The description of the plugin.
///
/// * `version` - The version of the plugin.
///
/// # Error
///
/// If fail to create the plugin descriptor, then an error will be returned.
pub fn new(
name: impl AsRef<str>,
desc: impl AsRef<str>,
version: PluginVersion,
) -> WasmEdgeResult<Self> {
Ok(Self {
inner: sys::plugin::PluginDescriptor::create(name, desc, version.inner)?,
})
}
/// Adds a module descriptor to the plugin descriptor.
///
/// # Arguments
///
/// * `name` - The name of the module.
///
/// * `desc` - The description of the module.
///
/// * `f` - The function that creates a module instance for the plugin.
///
/// # Error
///
/// If fail to add the module descriptor, then an error will be returned.
pub fn add_module_descriptor(
mut self,
name: impl AsRef<str>,
desc: impl AsRef<str>,
f: Option<ModuleInstanceCreateFn>,
) -> WasmEdgeResult<Self> {
self.inner = self.inner.add_module_descriptor(name, desc, f)?;
Ok(self)
}
/// Adds a program option to the plugin descriptor.
///
/// # Arguments
///
/// * `name` - The name of the program option.
///
/// * `desc` - The description of the program option.
///
/// * `ty` - The type of the program option.
///
/// # Error
///
/// If fail to add the program option, then an error will be returned.
pub fn add_program_option(
mut self,
name: impl AsRef<str>,
desc: impl AsRef<str>,
ty: ProgramOptionType,
) -> WasmEdgeResult<Self> {
self.inner = self.inner.add_program_option(name, desc, ty)?;
Ok(self)
}
/// Returns the raw pointer to the inner `WasmEdge_PluginDescriptor`.
#[cfg(feature = "ffi")]
pub fn as_raw_ptr(&self) -> *const sys::ffi::WasmEdge_PluginDescriptor {
self.inner.as_raw_ptr()
}
}
/// Defines the version of a plugin.
#[derive(Debug)]
pub struct PluginVersion {
pub inner: sys::plugin::PluginVersion,
}
impl PluginVersion {
/// Creates a new plugin version.
pub fn new(major: u32, minor: u32, patch: u32, build: u32) -> Self {
Self {
inner: sys::plugin::PluginVersion::create(major, minor, patch, build),
}
}
}