rhai/src/packages/fn_basic.rs

123 lines
3.5 KiB
Rust
Raw Normal View History

2020-08-14 12:58:34 +02:00
use crate::plugin::*;
2020-11-27 16:37:59 +01:00
use crate::{def_package, FnPtr, ImmutableString, NativeCallContext};
2020-11-23 12:11:32 +01:00
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
2020-11-25 02:36:06 +01:00
use crate::{ast::ScriptFnDef, stdlib::collections::HashMap, Array, Map};
2020-08-14 12:58:34 +02:00
2020-06-25 12:07:57 +02:00
def_package!(crate:BasicFnPackage:"Basic Fn functions.", lib, {
2020-09-13 16:12:11 +02:00
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
2020-06-25 12:07:57 +02:00
});
2020-08-20 16:11:41 +02:00
#[export_module]
mod fn_ptr_functions {
#[rhai_fn(name = "name", get = "name", pure)]
2020-08-24 04:38:15 +02:00
pub fn name(f: &mut FnPtr) -> ImmutableString {
2020-08-20 16:11:41 +02:00
f.get_fn_name().clone()
}
2020-10-18 16:10:08 +02:00
#[cfg(not(feature = "no_function"))]
2020-11-23 12:11:32 +01:00
pub mod functions {
#[rhai_fn(name = "is_anonymous", get = "is_anonymous", pure)]
2020-10-18 16:10:08 +02:00
pub fn is_anonymous(f: &mut FnPtr) -> bool {
f.is_anonymous()
}
}
2020-11-23 12:11:32 +01:00
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
pub mod functions_and_maps {
pub fn get_fn_metadata_list(ctx: NativeCallContext) -> Array {
collect_fn_metadata(ctx)
}
}
}
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
fn collect_fn_metadata(ctx: NativeCallContext) -> Array {
// Create a metadata record for a function.
fn make_metadata(
2020-11-23 14:12:57 +01:00
dict: &HashMap<&str, ImmutableString>,
2020-11-23 12:11:32 +01:00
namespace: Option<ImmutableString>,
2020-11-25 02:36:06 +01:00
f: &ScriptFnDef,
2020-11-23 12:11:32 +01:00
) -> Map {
let mut map = Map::with_capacity(6);
if let Some(ns) = namespace {
map.insert(dict["namespace"].clone(), ns.into());
}
map.insert(dict["name"].clone(), f.name.clone().into());
map.insert(
dict["access"].clone(),
match f.access {
FnAccess::Public => dict["public"].clone(),
FnAccess::Private => dict["private"].clone(),
}
.into(),
);
map.insert(
dict["is_anonymous"].clone(),
f.name.starts_with(crate::engine::FN_ANONYMOUS).into(),
);
map.insert(
dict["params"].clone(),
f.params
.iter()
.cloned()
.map(Into::<Dynamic>::into)
.collect::<Array>()
.into(),
);
map.into()
}
// Recursively scan modules for script-defined functions.
fn scan_module(
list: &mut Array,
2020-11-23 14:12:57 +01:00
dict: &HashMap<&str, ImmutableString>,
2020-11-23 12:11:32 +01:00
namespace: ImmutableString,
module: &Module,
) {
module.iter_script_fn().for_each(|(_, _, _, _, f)| {
list.push(make_metadata(dict, Some(namespace.clone()), f).into())
});
module.iter_sub_modules().for_each(|(ns, m)| {
let ns: ImmutableString = format!("{}::{}", namespace, ns).into();
scan_module(list, dict, ns, m.as_ref())
});
}
// Intern strings
2020-11-23 14:12:57 +01:00
let mut dict = HashMap::<&str, ImmutableString>::with_capacity(8);
[
"namespace",
"name",
"access",
"public",
"private",
"is_anonymous",
"params",
]
.iter()
.for_each(|&s| {
dict.insert(s, s.into());
});
2020-11-23 12:11:32 +01:00
let mut list: Array = Default::default();
2021-03-03 15:49:57 +01:00
ctx.iter_namespaces()
2020-11-23 12:11:32 +01:00
.flat_map(|m| m.iter_script_fn())
.for_each(|(_, _, _, _, f)| list.push(make_metadata(&dict, None, f).into()));
2021-03-03 15:49:57 +01:00
#[cfg(not(feature = "no_module"))]
ctx.iter_imports_raw()
.for_each(|(ns, m)| scan_module(&mut list, &dict, ns.clone(), m.as_ref()));
2020-11-23 12:11:32 +01:00
list
}