Change to struct variants for better flexibility.
This commit is contained in:
@@ -21,7 +21,7 @@ pub struct EncapsulatedEnviron {
|
||||
pub lib: crate::SharedModule,
|
||||
/// Imported [modules][crate::Module].
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
pub imports: Box<[(crate::ImmutableString, crate::SharedModule)]>,
|
||||
pub imports: Box<crate::StaticVec<(crate::ImmutableString, crate::SharedModule)>>,
|
||||
/// Globally-defined constants.
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
@@ -34,20 +34,38 @@ pub struct EncapsulatedEnviron {
|
||||
#[non_exhaustive]
|
||||
pub enum CallableFunction {
|
||||
/// A pure native Rust function with all arguments passed by value.
|
||||
Pure(Shared<FnAny>, bool),
|
||||
Pure {
|
||||
/// Shared function pointer.
|
||||
func: Shared<FnAny>,
|
||||
/// Does the function take a [`NativeCallContext`][crate::NativeCallContext] parameter?
|
||||
has_context: bool,
|
||||
},
|
||||
/// A native Rust object method with the first argument passed by reference,
|
||||
/// and the rest passed by value.
|
||||
Method(Shared<FnAny>, bool),
|
||||
Method {
|
||||
/// Shared function pointer.
|
||||
func: Shared<FnAny>,
|
||||
/// Does the function take a [`NativeCallContext`][crate::NativeCallContext] parameter?
|
||||
has_context: bool,
|
||||
},
|
||||
/// An iterator function.
|
||||
Iterator(Shared<IteratorFn>),
|
||||
Iterator {
|
||||
/// Shared function pointer.
|
||||
func: Shared<IteratorFn>,
|
||||
},
|
||||
/// A plugin function,
|
||||
Plugin(Shared<FnPlugin>),
|
||||
Plugin {
|
||||
/// Shared function pointer.
|
||||
func: Shared<FnPlugin>,
|
||||
},
|
||||
/// A script-defined function.
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Script(
|
||||
Shared<crate::ast::ScriptFnDef>,
|
||||
Option<Shared<EncapsulatedEnviron>>,
|
||||
),
|
||||
Script {
|
||||
/// Shared reference to the [`ScriptFnDef`][crate::ast::ScriptFnDef] function definition.
|
||||
fn_def: Shared<crate::ast::ScriptFnDef>,
|
||||
/// Encapsulated environment, if any.
|
||||
environ: Option<Shared<EncapsulatedEnviron>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl fmt::Debug for CallableFunction {
|
||||
@@ -55,13 +73,13 @@ impl fmt::Debug for CallableFunction {
|
||||
#[inline(never)]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Pure(..) => f.write_str("NativePureFunction"),
|
||||
Self::Method(..) => f.write_str("NativeMethod"),
|
||||
Self::Iterator(..) => f.write_str("NativeIterator"),
|
||||
Self::Plugin(..) => f.write_str("PluginFunction"),
|
||||
Self::Pure { .. } => f.write_str("NativePureFunction"),
|
||||
Self::Method { .. } => f.write_str("NativeMethod"),
|
||||
Self::Iterator { .. } => f.write_str("NativeIterator"),
|
||||
Self::Plugin { .. } => f.write_str("PluginFunction"),
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(fn_def, ..) => fmt::Debug::fmt(fn_def, f),
|
||||
Self::Script { fn_def, .. } => fmt::Debug::fmt(fn_def, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,13 +87,13 @@ impl fmt::Debug for CallableFunction {
|
||||
impl fmt::Display for CallableFunction {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Pure(..) => f.write_str("NativePureFunction"),
|
||||
Self::Method(..) => f.write_str("NativeMethod"),
|
||||
Self::Iterator(..) => f.write_str("NativeIterator"),
|
||||
Self::Plugin(..) => f.write_str("PluginFunction"),
|
||||
Self::Pure { .. } => f.write_str("NativePureFunction"),
|
||||
Self::Method { .. } => f.write_str("NativeMethod"),
|
||||
Self::Iterator { .. } => f.write_str("NativeIterator"),
|
||||
Self::Plugin { .. } => f.write_str("PluginFunction"),
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(fn_def, ..) => fmt::Display::fmt(fn_def, f),
|
||||
Self::Script { fn_def, .. } => fmt::Display::fmt(fn_def, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,13 +104,13 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn is_pure(&self) -> bool {
|
||||
match self {
|
||||
Self::Pure(..) => true,
|
||||
Self::Method(..) | Self::Iterator(..) => false,
|
||||
Self::Pure { .. } => true,
|
||||
Self::Method { .. } | Self::Iterator { .. } => false,
|
||||
|
||||
Self::Plugin(p) => !p.is_method_call(),
|
||||
Self::Plugin { func, .. } => !func.is_method_call(),
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => false,
|
||||
Self::Script { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Is this a native Rust method function?
|
||||
@@ -100,13 +118,13 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn is_method(&self) -> bool {
|
||||
match self {
|
||||
Self::Method(..) => true,
|
||||
Self::Pure(..) | Self::Iterator(..) => false,
|
||||
Self::Method { .. } => true,
|
||||
Self::Pure { .. } | Self::Iterator { .. } => false,
|
||||
|
||||
Self::Plugin(p) => p.is_method_call(),
|
||||
Self::Plugin { func, .. } => func.is_method_call(),
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => false,
|
||||
Self::Script { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Is this an iterator function?
|
||||
@@ -114,11 +132,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub const fn is_iter(&self) -> bool {
|
||||
match self {
|
||||
Self::Iterator(..) => true,
|
||||
Self::Pure(..) | Self::Method(..) | Self::Plugin(..) => false,
|
||||
Self::Iterator { .. } => true,
|
||||
Self::Pure { .. } | Self::Method { .. } | Self::Plugin { .. } => false,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => false,
|
||||
Self::Script { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Is this a script-defined function?
|
||||
@@ -130,8 +148,11 @@ impl CallableFunction {
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
match self {
|
||||
Self::Script(..) => true,
|
||||
Self::Pure(..) | Self::Method(..) | Self::Iterator(..) | Self::Plugin(..) => false,
|
||||
Self::Script { .. } => true,
|
||||
Self::Pure { .. }
|
||||
| Self::Method { .. }
|
||||
| Self::Iterator { .. }
|
||||
| Self::Plugin { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Is this a plugin function?
|
||||
@@ -139,11 +160,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub const fn is_plugin_fn(&self) -> bool {
|
||||
match self {
|
||||
Self::Plugin(..) => true,
|
||||
Self::Pure(..) | Self::Method(..) | Self::Iterator(..) => false,
|
||||
Self::Plugin { .. } => true,
|
||||
Self::Pure { .. } | Self::Method { .. } | Self::Iterator { .. } => false,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => false,
|
||||
Self::Script { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Is this a native Rust function?
|
||||
@@ -155,8 +176,11 @@ impl CallableFunction {
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
match self {
|
||||
Self::Pure(..) | Self::Method(..) | Self::Plugin(..) | Self::Iterator(..) => true,
|
||||
Self::Script(..) => false,
|
||||
Self::Pure { .. }
|
||||
| Self::Method { .. }
|
||||
| Self::Plugin { .. }
|
||||
| Self::Iterator { .. } => true,
|
||||
Self::Script { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Is there a [`NativeCallContext`][crate::NativeCallContext] parameter?
|
||||
@@ -164,11 +188,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn has_context(&self) -> bool {
|
||||
match self {
|
||||
Self::Pure(.., ctx) | Self::Method(.., ctx) => *ctx,
|
||||
Self::Plugin(f) => f.has_context(),
|
||||
Self::Iterator(..) => false,
|
||||
Self::Pure { has_context, .. } | Self::Method { has_context, .. } => *has_context,
|
||||
Self::Plugin { func, .. } => func.has_context(),
|
||||
Self::Iterator { .. } => false,
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => false,
|
||||
Self::Script { .. } => false,
|
||||
}
|
||||
}
|
||||
/// Get the access mode.
|
||||
@@ -180,10 +204,11 @@ impl CallableFunction {
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
match self {
|
||||
Self::Plugin(..) | Self::Pure(..) | Self::Method(..) | Self::Iterator(..) => {
|
||||
FnAccess::Public
|
||||
}
|
||||
Self::Script(f, ..) => f.access,
|
||||
Self::Plugin { .. }
|
||||
| Self::Pure { .. }
|
||||
| Self::Method { .. }
|
||||
| Self::Iterator { .. } => FnAccess::Public,
|
||||
Self::Script { fn_def, .. } => fn_def.access,
|
||||
}
|
||||
}
|
||||
/// Get a shared reference to a native Rust function.
|
||||
@@ -191,11 +216,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn get_native_fn(&self) -> Option<&Shared<FnAny>> {
|
||||
match self {
|
||||
Self::Pure(f, ..) | Self::Method(f, ..) => Some(f),
|
||||
Self::Iterator(..) | Self::Plugin(..) => None,
|
||||
Self::Pure { func, .. } | Self::Method { func, .. } => Some(func),
|
||||
Self::Iterator { .. } | Self::Plugin { .. } => None,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => None,
|
||||
Self::Script { .. } => None,
|
||||
}
|
||||
}
|
||||
/// Get a shared reference to a script-defined function definition.
|
||||
@@ -206,8 +231,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub const fn get_script_fn_def(&self) -> Option<&Shared<crate::ast::ScriptFnDef>> {
|
||||
match self {
|
||||
Self::Pure(..) | Self::Method(..) | Self::Iterator(..) | Self::Plugin(..) => None,
|
||||
Self::Script(f, ..) => Some(f),
|
||||
Self::Pure { .. }
|
||||
| Self::Method { .. }
|
||||
| Self::Iterator { .. }
|
||||
| Self::Plugin { .. } => None,
|
||||
Self::Script { fn_def, .. } => Some(fn_def),
|
||||
}
|
||||
}
|
||||
/// Get a reference to the shared encapsulated environment of the function definition.
|
||||
@@ -217,10 +245,13 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn get_encapsulated_environ(&self) -> Option<&EncapsulatedEnviron> {
|
||||
match self {
|
||||
Self::Pure(..) | Self::Method(..) | Self::Iterator(..) | Self::Plugin(..) => None,
|
||||
Self::Pure { .. }
|
||||
| Self::Method { .. }
|
||||
| Self::Iterator { .. }
|
||||
| Self::Plugin { .. } => None,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(.., environ) => environ.as_deref(),
|
||||
Self::Script { environ, .. } => environ.as_deref(),
|
||||
}
|
||||
}
|
||||
/// Get a reference to an iterator function.
|
||||
@@ -228,11 +259,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn get_iter_fn(&self) -> Option<&IteratorFn> {
|
||||
match self {
|
||||
Self::Iterator(f) => Some(&**f),
|
||||
Self::Pure(..) | Self::Method(..) | Self::Plugin(..) => None,
|
||||
Self::Iterator { func, .. } => Some(&**func),
|
||||
Self::Pure { .. } | Self::Method { .. } | Self::Plugin { .. } => None,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => None,
|
||||
Self::Script { .. } => None,
|
||||
}
|
||||
}
|
||||
/// Get a shared reference to a plugin function.
|
||||
@@ -240,11 +271,11 @@ impl CallableFunction {
|
||||
#[must_use]
|
||||
pub fn get_plugin_fn(&self) -> Option<&Shared<FnPlugin>> {
|
||||
match self {
|
||||
Self::Plugin(f) => Some(f),
|
||||
Self::Pure(..) | Self::Method(..) | Self::Iterator(..) => None,
|
||||
Self::Plugin { func, .. } => Some(func),
|
||||
Self::Pure { .. } | Self::Method { .. } | Self::Iterator { .. } => None,
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Self::Script(..) => None,
|
||||
Self::Script { .. } => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,29 +283,37 @@ impl CallableFunction {
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
impl From<crate::ast::ScriptFnDef> for CallableFunction {
|
||||
#[inline(always)]
|
||||
fn from(func: crate::ast::ScriptFnDef) -> Self {
|
||||
Self::Script(func.into(), None)
|
||||
fn from(fn_def: crate::ast::ScriptFnDef) -> Self {
|
||||
Self::Script {
|
||||
fn_def: fn_def.into(),
|
||||
environ: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
impl From<Shared<crate::ast::ScriptFnDef>> for CallableFunction {
|
||||
#[inline(always)]
|
||||
fn from(func: Shared<crate::ast::ScriptFnDef>) -> Self {
|
||||
Self::Script(func, None)
|
||||
fn from(fn_def: Shared<crate::ast::ScriptFnDef>) -> Self {
|
||||
Self::Script {
|
||||
fn_def,
|
||||
environ: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PluginFunction + 'static + SendSync> From<T> for CallableFunction {
|
||||
#[inline(always)]
|
||||
fn from(func: T) -> Self {
|
||||
Self::Plugin(Shared::new(func))
|
||||
Self::Plugin {
|
||||
func: Shared::new(func),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Shared<FnPlugin>> for CallableFunction {
|
||||
#[inline(always)]
|
||||
fn from(func: Shared<FnPlugin>) -> Self {
|
||||
Self::Plugin(func)
|
||||
Self::Plugin { func }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user