374 lines
11 KiB
Rust
Raw Normal View History

//! Module that defines functions to output definition files for [`Engine`].
#![cfg(feature = "metadata")]
use crate::module::FuncInfo;
use crate::plugin::*;
use crate::tokenizer::is_valid_function_name;
use crate::{Engine, Module, Scope, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{any::type_name, borrow::Cow, cmp::Ordering, fmt};
2022-07-25 19:01:06 +02:00
impl Engine {
/// Return [`Definitions`] that can be used to generate definition files for the [`Engine`].
/// Exported under the `metadata` feature only.
2022-07-25 19:01:06 +02:00
///
/// # Example
///
/// ```no_run
/// # use rhai::Engine;
/// # fn main() -> std::io::Result<()> {
2022-07-25 19:01:06 +02:00
/// let engine = Engine::new();
///
2022-07-25 19:01:06 +02:00
/// engine
/// .definitions()
/// .write_to_dir(".rhai/definitions")?;
/// # Ok(())
/// # }
/// ```
#[inline(always)]
2022-07-25 19:01:06 +02:00
pub fn definitions(&self) -> Definitions {
Definitions {
engine: self,
scope: None,
}
}
/// Return [`Definitions`] that can be used to generate definition files for the [`Engine`] and
/// the given [`Scope`].
/// Exported under the `metadata` feature only.
2022-07-25 19:01:06 +02:00
///
/// # Example
///
/// ```no_run
/// # use rhai::{Engine, Scope};
/// # fn main() -> std::io::Result<()> {
2022-07-25 19:01:06 +02:00
/// let engine = Engine::new();
/// let scope = Scope::new();
/// engine
/// .definitions_with_scope(&scope)
/// .write_to_dir(".rhai/definitions")?;
/// # Ok(())
/// # }
/// ```
#[inline(always)]
2022-07-25 19:01:06 +02:00
pub fn definitions_with_scope<'e>(&'e self, scope: &'e Scope<'e>) -> Definitions<'e> {
Definitions {
engine: self,
scope: Some(scope),
}
}
}
/// Definitions helper type to generate definition files based on the contents of an [`Engine`].
2022-07-25 19:01:06 +02:00
#[must_use]
pub struct Definitions<'e> {
/// The [`Engine`].
2022-07-25 19:01:06 +02:00
engine: &'e Engine,
/// Optional [`Scope`] to include.
2022-07-25 19:01:06 +02:00
scope: Option<&'e Scope<'e>>,
}
impl<'e> Definitions<'e> {
/// Output all definition files returned from [`iter_files`][Definitions::iter_files] to a
/// specified directory.
2022-07-25 19:01:06 +02:00
///
/// This function creates the directory if it does not exist, and overrides any existing files.
2022-07-26 14:51:22 +02:00
#[cfg(all(not(feature = "no_std"), not(target_family = "wasm")))]
pub fn write_to_dir(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
use std::fs;
2022-07-25 19:01:06 +02:00
let path = path.as_ref();
fs::create_dir_all(path)?;
fs::write(
path.join("__builtin__.d.rhai"),
include_bytes!("builtin.d.rhai"),
)?;
fs::write(
path.join("__builtin-operators__.d.rhai"),
include_bytes!("builtin-operators.d.rhai"),
)?;
2022-07-26 13:39:50 +02:00
2022-07-25 19:01:06 +02:00
fs::write(path.join("__static__.d.rhai"), self.static_module())?;
if self.scope.is_some() {
fs::write(path.join("__scope__.d.rhai"), self.scope())?;
}
#[cfg(not(feature = "no_module"))]
2022-07-25 19:01:06 +02:00
for (name, decl) in self.modules() {
fs::write(path.join(format!("{name}.d.rhai")), decl)?;
}
Ok(())
}
/// Iterate over generated definition files.
2022-07-26 14:16:54 +02:00
///
/// The returned iterator yields all definition files as (filename, content) pairs.
#[inline]
2022-07-26 14:16:54 +02:00
pub fn iter_files(&self) -> impl Iterator<Item = (String, String)> + '_ {
IntoIterator::into_iter([
(
"__builtin__.d.rhai".to_string(),
include_str!("builtin.d.rhai").to_string(),
),
(
"__builtin-operators__.d.rhai".to_string(),
include_str!("builtin-operators.d.rhai").to_string(),
),
("__static__.d.rhai".to_string(), self.static_module()),
])
.chain(
self.scope
.iter()
.map(move |_| ("__scope__.d.rhai".to_string(), self.scope())),
)
.chain(
#[cfg(not(feature = "no_module"))]
{
self.modules()
.map(|(name, def)| (format!("{name}.d.rhai"), def))
},
#[cfg(feature = "no_module")]
{
std::iter::empty()
},
2022-07-26 14:16:54 +02:00
)
}
/// Return definitions for all globally available functions.
2022-07-25 19:01:06 +02:00
///
/// Always starts with `module static;`.
2022-07-25 19:01:06 +02:00
#[must_use]
pub fn static_module(&self) -> String {
let mut s = String::from("module static;\n\n");
let mut first = true;
for m in &self.engine.global_modules {
if !first {
s += "\n\n";
}
first = false;
m.write_definition(&mut s, self).unwrap();
2022-07-25 19:01:06 +02:00
}
s
}
/// Return definitions for all items inside the [`Scope`], if any.
2022-07-25 19:01:06 +02:00
///
/// Always starts with `module static;` even if the [`Scope`] is empty or none was provided.
2022-07-25 19:01:06 +02:00
#[must_use]
pub fn scope(&self) -> String {
let mut s = String::from("module static;\n\n");
if let Some(scope) = self.scope {
scope.write_definition(&mut s).unwrap();
2022-07-25 19:01:06 +02:00
}
s
}
/// Return a (module name, definitions) pair for each registered static [module][Module].
2022-07-25 19:01:06 +02:00
///
/// Not available under `no_module`.
2022-07-26 14:16:54 +02:00
///
/// Always starts with `module <module name>;`.
#[cfg(not(feature = "no_module"))]
2022-07-25 19:01:06 +02:00
pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ {
let mut m = self
.engine
.global_sub_modules
.iter()
.map(move |(name, module)| {
(
name.to_string(),
format!("module {name};\n\n{}", module.definition(self)),
)
})
.collect::<Vec<_>>();
m.sort_by(|(name1, _), (name2, _)| name1.cmp(name2));
2022-07-25 19:01:06 +02:00
m.into_iter()
}
}
impl Module {
/// Return definitions for all items inside the [`Module`].
fn definition(&self, def: &Definitions) -> String {
2022-07-25 19:01:06 +02:00
let mut s = String::new();
self.write_definition(&mut s, def).unwrap();
2022-07-25 19:01:06 +02:00
s
}
/// Output definitions for all items inside the [`Module`].
fn write_definition(&self, writer: &mut dyn fmt::Write, def: &Definitions) -> fmt::Result {
2022-07-25 19:01:06 +02:00
let mut first = true;
let mut vars = self.iter_var().collect::<Vec<_>>();
vars.sort_by(|(a, _), (b, _)| a.cmp(b));
for (name, _) in vars {
if !first {
writer.write_str("\n\n")?;
}
first = false;
write!(writer, "const {name}: ?;")?;
}
let mut func_infos = self.iter_fn().collect::<Vec<_>>();
func_infos.sort_by(|a, b| match a.metadata.name.cmp(&b.metadata.name) {
Ordering::Equal => match a.metadata.params.cmp(&b.metadata.params) {
Ordering::Equal => (a.metadata.params_info.join("")
+ a.metadata.return_type.as_str())
.cmp(&(b.metadata.params_info.join("") + b.metadata.return_type.as_str())),
o => o,
},
o => o,
});
2022-07-25 19:01:06 +02:00
for f in func_infos {
if !first {
writer.write_str("\n\n")?;
}
first = false;
if f.metadata.access == FnAccess::Private {
continue;
}
#[cfg(not(feature = "no_custom_syntax"))]
let operator = def.engine.custom_keywords.contains_key(&f.metadata.name)
|| (!f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name));
#[cfg(feature = "no_custom_syntax")]
let operator =
!f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name);
f.write_definition(writer, def, operator)?;
2022-07-25 19:01:06 +02:00
}
Ok(())
}
}
impl FuncInfo {
/// Output definitions for a function.
fn write_definition(
&self,
writer: &mut dyn fmt::Write,
def: &Definitions,
operator: bool,
) -> fmt::Result {
2022-07-25 19:01:06 +02:00
for comment in &*self.metadata.comments {
writeln!(writer, "{comment}")?;
}
if operator {
writer.write_str("op ")?;
} else {
writer.write_str("fn ")?;
}
if let Some(name) = self.metadata.name.strip_prefix("get$") {
write!(writer, "get {name}(")?;
} else if let Some(name) = self.metadata.name.strip_prefix("set$") {
write!(writer, "set {name}(")?;
} else {
write!(writer, "{}(", self.metadata.name)?;
}
let mut first = true;
for i in 0..self.metadata.params {
if !first {
writer.write_str(", ")?;
}
first = false;
2022-07-27 18:04:59 +08:00
let (param_name, param_type) =
self.metadata
.params_info
.get(i)
.map_or(("_", "?".into()), |s| {
let mut s = s.splitn(2, ':');
(
s.next().unwrap_or("_").split(' ').last().unwrap(),
s.next()
.map_or(Cow::Borrowed("?"), |ty| def_type_name(ty, def.engine)),
)
});
2022-07-25 19:01:06 +02:00
if operator {
write!(writer, "{param_type}")?;
} else {
write!(writer, "{param_name}: {param_type}")?;
}
}
write!(
writer,
") -> {};",
def_type_name(&self.metadata.return_type, def.engine)
2022-07-25 19:01:06 +02:00
)?;
Ok(())
}
}
/// We have to transform some of the types.
///
/// This is highly inefficient and is currently based on trial and error with the core packages.
2022-07-25 19:01:06 +02:00
///
/// It tries to flatten types, removing `&` and `&mut`, and paths, while keeping generics.
2022-07-25 19:01:06 +02:00
///
/// Associated generic types are also rewritten into regular generic type parameters.
fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> {
let ty = engine.format_type_name(ty).replace("crate::", "");
let ty = ty.strip_prefix("&mut").unwrap_or(&*ty).trim();
2022-07-25 19:01:06 +02:00
let ty = ty.split("::").last().unwrap();
let ty = ty
.strip_prefix("RhaiResultOf<")
.and_then(|s| s.strip_suffix('>'))
2022-07-27 18:04:59 +08:00
.map_or(ty, str::trim);
2022-07-25 19:01:06 +02:00
let ty = ty
.replace("Iterator<Item=", "Iterator<")
2022-07-25 19:01:06 +02:00
.replace("Dynamic", "?")
.replace("INT", "int")
.replace(type_name::<INT>(), "int")
2022-07-25 19:01:06 +02:00
.replace("FLOAT", "float")
.replace("&str", "String")
.replace("ImmutableString", "String");
#[cfg(not(feature = "no_float"))]
let ty = ty.replace(type_name::<crate::FLOAT>(), "float");
ty.into()
2022-07-25 19:01:06 +02:00
}
impl Scope<'_> {
/// Return definitions for all items inside the [`Scope`].
fn write_definition(&self, writer: &mut dyn fmt::Write) -> fmt::Result {
2022-07-25 19:01:06 +02:00
let mut first = true;
for (name, constant, _) in self.iter_raw() {
if !first {
writer.write_str("\n\n")?;
}
first = false;
let kw = if constant { "const" } else { "let" };
write!(writer, "{kw} {name};")?;
}
Ok(())
}
}