selfhost: add ternary parser and plugin prefix guard
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use super::types::{PluginBoxV2, PluginHandleInner, LoadedPluginV2};
|
||||
use super::types::{PluginBoxMetadata, PluginBoxV2, PluginHandleInner, LoadedPluginV2};
|
||||
use crate::bid::{BidResult, BidError};
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::config::nyash_toml_v2::{NyashConfigV2, LibraryDefinition};
|
||||
@ -146,6 +146,40 @@ impl PluginLoaderV2 {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn metadata_for_type_id(&self, type_id: u32) -> Option<PluginBoxMetadata> {
|
||||
let config = self.config.as_ref()?;
|
||||
let cfg_path = self.config_path.as_ref()?;
|
||||
let toml_str = std::fs::read_to_string(cfg_path).ok()?;
|
||||
let toml_value: toml::Value = toml::from_str(&toml_str).ok()?;
|
||||
let (lib_name, box_type) = self.find_box_by_type_id(config, &toml_value, type_id)?;
|
||||
let plugin = {
|
||||
let plugins = self.plugins.read().ok()?;
|
||||
plugins.get(lib_name)?.clone()
|
||||
};
|
||||
let spec_key = (lib_name.to_string(), box_type.to_string());
|
||||
let mut resolved_type = type_id;
|
||||
let mut fini_method = None;
|
||||
if let Some(spec) = self.box_specs.read().ok()?.get(&spec_key).cloned() {
|
||||
if let Some(tid) = spec.type_id { resolved_type = tid; }
|
||||
if let Some(fini) = spec.fini_method_id { fini_method = Some(fini); }
|
||||
}
|
||||
if resolved_type == type_id || fini_method.is_none() {
|
||||
if let Some(cfg) = config.get_box_config(lib_name, box_type, &toml_value) {
|
||||
if resolved_type == type_id { resolved_type = cfg.type_id; }
|
||||
if fini_method.is_none() {
|
||||
fini_method = cfg.methods.get("fini").map(|m| m.method_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(PluginBoxMetadata {
|
||||
lib_name: lib_name.to_string(),
|
||||
box_type: box_type.to_string(),
|
||||
type_id: resolved_type,
|
||||
invoke_fn: plugin.invoke_fn,
|
||||
fini_method_id: fini_method,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn construct_existing_instance(&self, type_id: u32, instance_id: u32) -> Option<Box<dyn NyashBox>> {
|
||||
let config = self.config.as_ref()?;
|
||||
let cfg_path = self.config_path.as_ref()?;
|
||||
|
||||
@ -4,8 +4,14 @@ mod globals;
|
||||
mod errors;
|
||||
mod host_bridge;
|
||||
|
||||
pub use types::{PluginBoxV2, PluginHandleInner, NyashTypeBoxFfi, make_plugin_box_v2, construct_plugin_box};
|
||||
pub use types::{PluginBoxMetadata, PluginBoxV2, PluginHandleInner, NyashTypeBoxFfi, make_plugin_box_v2, construct_plugin_box};
|
||||
pub use loader::PluginLoaderV2;
|
||||
pub use globals::{get_global_loader_v2, init_global_loader_v2, shutdown_plugins_v2};
|
||||
|
||||
pub fn metadata_for_type_id(type_id: u32) -> Option<PluginBoxMetadata> {
|
||||
let loader = get_global_loader_v2();
|
||||
let guard = loader.read().ok()?;
|
||||
guard.metadata_for_type_id(type_id)
|
||||
}
|
||||
|
||||
pub fn backend_kind() -> &'static str { "enabled" }
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::box_trait::{NyashBox, BoxCore, StringBox};
|
||||
use crate::box_trait::{BoxCore, NyashBox, StringBox};
|
||||
use super::host_bridge::InvokeFn;
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -10,14 +11,23 @@ pub struct LoadedPluginV2 {
|
||||
pub(super) box_types: Vec<String>,
|
||||
pub(super) typeboxes: std::collections::HashMap<String, usize>,
|
||||
pub(super) init_fn: Option<unsafe extern "C" fn() -> i32>,
|
||||
pub(super) invoke_fn: unsafe extern "C" fn(u32, u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32,
|
||||
pub(super) invoke_fn: InvokeFn,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PluginBoxMetadata {
|
||||
pub lib_name: String,
|
||||
pub box_type: String,
|
||||
pub type_id: u32,
|
||||
pub invoke_fn: InvokeFn,
|
||||
pub fini_method_id: Option<u32>,
|
||||
}
|
||||
|
||||
/// v2 Plugin Box handle core
|
||||
#[derive(Debug)]
|
||||
pub struct PluginHandleInner {
|
||||
pub type_id: u32,
|
||||
pub invoke_fn: unsafe extern "C" fn(u32, u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32,
|
||||
pub invoke_fn: InvokeFn,
|
||||
pub instance_id: u32,
|
||||
pub fini_method_id: Option<u32>,
|
||||
pub(super) finalized: std::sync::atomic::AtomicBool,
|
||||
@ -104,7 +114,7 @@ impl PluginBoxV2 {
|
||||
}
|
||||
|
||||
/// Helper to construct a PluginBoxV2 from raw ids and invoke pointer safely
|
||||
pub fn make_plugin_box_v2(box_type: String, type_id: u32, instance_id: u32, invoke_fn: unsafe extern "C" fn(u32,u32,u32,*const u8,usize,*mut u8,*mut usize) -> i32) -> PluginBoxV2 {
|
||||
pub fn make_plugin_box_v2(box_type: String, type_id: u32, instance_id: u32, invoke_fn: InvokeFn) -> PluginBoxV2 {
|
||||
PluginBoxV2 { box_type, inner: Arc::new(PluginHandleInner { type_id, invoke_fn, instance_id, fini_method_id: None, finalized: std::sync::atomic::AtomicBool::new(false) }) }
|
||||
}
|
||||
|
||||
@ -112,7 +122,7 @@ pub fn make_plugin_box_v2(box_type: String, type_id: u32, instance_id: u32, invo
|
||||
pub fn construct_plugin_box(
|
||||
box_type: String,
|
||||
type_id: u32,
|
||||
invoke_fn: unsafe extern "C" fn(u32, u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32,
|
||||
invoke_fn: InvokeFn,
|
||||
instance_id: u32,
|
||||
fini_method_id: Option<u32>,
|
||||
) -> PluginBoxV2 {
|
||||
|
||||
@ -3,15 +3,27 @@ use crate::box_trait::NyashBox;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub type InvokeFn = unsafe extern "C" fn(u32, u32, u32, *const u8, usize, *mut u8, *mut usize) -> i32;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PluginBoxV2 {
|
||||
pub box_type: String,
|
||||
pub inner: std::sync::Arc<PluginHandleInner>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PluginBoxMetadata {
|
||||
pub lib_name: String,
|
||||
pub box_type: String,
|
||||
pub type_id: u32,
|
||||
pub invoke_fn: InvokeFn,
|
||||
pub fini_method_id: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PluginHandleInner {
|
||||
pub type_id: u32,
|
||||
pub invoke_fn: InvokeFn,
|
||||
pub instance_id: u32,
|
||||
pub fini_method_id: Option<u32>,
|
||||
}
|
||||
@ -25,6 +37,7 @@ impl PluginLoaderV2 {
|
||||
pub fn create_box(&self, _t: &str, _a: &[Box<dyn NyashBox>]) -> BidResult<Box<dyn NyashBox>> { Err(BidError::PluginError) }
|
||||
pub fn extern_call(&self, _iface_name: &str, _method_name: &str, _args: &[Box<dyn NyashBox>]) -> BidResult<Option<Box<dyn NyashBox>>> { Err(BidError::PluginError) }
|
||||
pub fn invoke_instance_method(&self, _box_type: &str, _method_name: &str, _instance_id: u32, _args: &[Box<dyn NyashBox>]) -> BidResult<Option<Box<dyn NyashBox>>> { Err(BidError::PluginError) }
|
||||
pub fn metadata_for_type_id(&self, _type_id: u32) -> Option<PluginBoxMetadata> { None }
|
||||
pub fn shutdown_singletons(&self) {}
|
||||
}
|
||||
|
||||
@ -34,3 +47,25 @@ pub fn init_global_loader_v2(_config_path: &str) -> BidResult<()> { Ok(()) }
|
||||
pub fn shutdown_plugins_v2() -> BidResult<()> { Ok(()) }
|
||||
|
||||
pub fn backend_kind() -> &'static str { "stub" }
|
||||
|
||||
pub fn metadata_for_type_id(_type_id: u32) -> Option<PluginBoxMetadata> { None }
|
||||
|
||||
pub fn make_plugin_box_v2(box_type: String, type_id: u32, instance_id: u32, invoke_fn: InvokeFn) -> PluginBoxV2 {
|
||||
PluginBoxV2 {
|
||||
box_type,
|
||||
inner: Arc::new(PluginHandleInner { type_id, invoke_fn, instance_id, fini_method_id: None }),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn construct_plugin_box(
|
||||
box_type: String,
|
||||
type_id: u32,
|
||||
invoke_fn: InvokeFn,
|
||||
instance_id: u32,
|
||||
fini_method_id: Option<u32>,
|
||||
) -> PluginBoxV2 {
|
||||
PluginBoxV2 {
|
||||
box_type,
|
||||
inner: Arc::new(PluginHandleInner { type_id, invoke_fn, instance_id, fini_method_id }),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user