#![cfg(feature = "internals")]
#![cfg(feature = "metadata")]
use crate::module::{FuncInfo, ModuleFlags};
use crate::tokenizer::{is_valid_function_name, Token};
use crate::{Engine, FnAccess, FnPtr, Module, Scope, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{any::type_name, borrow::Cow, cmp::Ordering, fmt};
impl Engine {
#[inline(always)]
#[must_use]
pub fn definitions(&self) -> Definitions {
Definitions {
engine: self,
scope: None,
config: DefinitionsConfig::default(),
}
}
#[inline(always)]
#[must_use]
pub fn definitions_with_scope<'e>(&'e self, scope: &'e Scope<'e>) -> Definitions<'e> {
Definitions {
engine: self,
scope: Some(scope),
config: DefinitionsConfig::default(),
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub struct DefinitionsConfig {
pub write_headers: bool,
pub include_standard_packages: bool,
}
impl Default for DefinitionsConfig {
#[inline(always)]
#[must_use]
fn default() -> Self {
Self {
write_headers: false,
include_standard_packages: true,
}
}
}
#[derive(Debug, Clone)]
pub struct Definitions<'e> {
engine: &'e Engine,
scope: Option<&'e Scope<'e>>,
config: DefinitionsConfig,
}
impl Definitions<'_> {
#[inline(always)]
#[must_use]
pub const fn with_headers(mut self, headers: bool) -> Self {
self.config.write_headers = headers;
self
}
#[inline(always)]
#[must_use]
pub const fn include_standard_packages(mut self, include_standard_packages: bool) -> Self {
self.config.include_standard_packages = include_standard_packages;
self
}
#[inline(always)]
#[must_use]
pub const fn engine(&self) -> &Engine {
self.engine
}
#[inline(always)]
#[must_use]
pub const fn scope(&self) -> Option<&Scope> {
self.scope
}
#[inline(always)]
#[must_use]
pub(crate) const fn config(&self) -> &DefinitionsConfig {
&self.config
}
}
impl Definitions<'_> {
#[cfg(all(not(feature = "no_std"), not(target_family = "wasm")))]
#[inline]
pub fn write_to_dir(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
use std::fs;
let path = path.as_ref();
fs::create_dir_all(path)?;
for (file_name, content) in self.iter_files() {
fs::write(path.join(file_name), content)?;
}
Ok(())
}
#[cfg(all(not(feature = "no_std"), not(target_family = "wasm")))]
#[inline(always)]
pub fn write_to_file(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
std::fs::write(path, self.single_file())
}
#[inline]
#[must_use]
pub fn single_file(&self) -> String {
let config = DefinitionsConfig {
write_headers: false,
..self.config
};
let mut def_file = String::from("module static;\n\n");
if config.include_standard_packages {
def_file += &Self::builtin_functions_operators_impl(config);
def_file += "\n";
def_file += &Self::builtin_functions_impl(config);
def_file += "\n";
}
def_file += &self.static_module_impl(config);
def_file += "\n";
#[cfg(not(feature = "no_module"))]
{
use std::fmt::Write;
for (module_name, module_def) in self.modules_impl(config) {
write!(
&mut def_file,
"\nmodule {module_name} {{\n{module_def}\n}}\n"
)
.unwrap();
}
def_file += "\n";
}
def_file += &self.scope_items_impl(config);
def_file += "\n";
def_file
}
#[inline]
pub fn iter_files(&self) -> impl Iterator<Item = (String, String)> + '_ {
let config = DefinitionsConfig {
write_headers: true,
..self.config
};
if config.include_standard_packages {
vec![
(
"__builtin__.d.rhai".to_string(),
Self::builtin_functions_impl(config),
),
(
"__builtin-operators__.d.rhai".to_string(),
Self::builtin_functions_operators_impl(config),
),
]
} else {
vec![]
}
.into_iter()
.chain(std::iter::once((
"__static__.d.rhai".to_string(),
self.static_module_impl(config),
)))
.chain(self.scope.iter().map(move |_| {
(
"__scope__.d.rhai".to_string(),
self.scope_items_impl(config),
)
}))
.chain(
#[cfg(not(feature = "no_module"))]
{
self.modules_impl(config)
.map(|(name, def)| (format!("{name}.d.rhai"), def))
},
#[cfg(feature = "no_module")]
{
std::iter::empty()
},
)
}
#[inline(always)]
#[must_use]
pub fn builtin_functions(&self) -> String {
Self::builtin_functions_impl(self.config)
}
#[must_use]
fn builtin_functions_impl(config: DefinitionsConfig) -> String {
let def = include_str!("builtin-functions.d.rhai");
if config.write_headers {
format!("module static;\n\n{def}")
} else {
def.to_string()
}
}
#[inline(always)]
#[must_use]
pub fn builtin_functions_operators(&self) -> String {
Self::builtin_functions_operators_impl(self.config)
}
#[must_use]
fn builtin_functions_operators_impl(config: DefinitionsConfig) -> String {
let def = include_str!("builtin-operators.d.rhai");
if config.write_headers {
format!("module static;\n\n{def}")
} else {
def.to_string()
}
}
#[inline(always)]
#[must_use]
pub fn static_module(&self) -> String {
self.static_module_impl(self.config)
}
#[must_use]
fn static_module_impl(&self, config: DefinitionsConfig) -> String {
let mut s = if config.write_headers {
String::from("module static;\n\n")
} else {
String::new()
};
let exclude_flags = if self.config.include_standard_packages {
ModuleFlags::empty()
} else {
ModuleFlags::STANDARD_LIB
};
self.engine
.global_modules
.iter()
.filter(|m| !m.flags.contains(exclude_flags))
.enumerate()
.for_each(|(i, m)| {
if i > 0 {
s += "\n\n";
}
m.write_definition(&mut s, self).unwrap();
});
s
}
#[inline(always)]
#[must_use]
pub fn scope_items(&self) -> String {
self.scope_items_impl(self.config)
}
#[must_use]
fn scope_items_impl(&self, config: DefinitionsConfig) -> String {
let mut s = if config.write_headers {
String::from("module static;\n\n")
} else {
String::new()
};
if let Some(scope) = self.scope {
scope.write_definition(&mut s, self).unwrap();
}
s
}
#[cfg(not(feature = "no_module"))]
#[inline(always)]
pub fn modules(&self) -> impl Iterator<Item = (String, String)> + '_ {
self.modules_impl(self.config)
}
#[cfg(not(feature = "no_module"))]
fn modules_impl(
&self,
config: DefinitionsConfig,
) -> impl Iterator<Item = (String, String)> + '_ {
let mut m = self
.engine
.global_sub_modules
.as_ref()
.into_iter()
.flatten()
.map(move |(name, module)| {
(
name.to_string(),
if config.write_headers {
format!("module {name};\n\n{}", module.definition(self))
} else {
module.definition(self)
},
)
})
.collect::<Vec<_>>();
m.sort_by(|(name1, _), (name2, _)| name1.cmp(name2));
m.into_iter()
}
}
impl Module {
#[cfg(not(feature = "no_module"))]
#[must_use]
fn definition(&self, def: &Definitions) -> String {
let mut s = String::new();
self.write_definition(&mut s, def).unwrap();
s
}
fn write_definition(&self, writer: &mut dyn fmt::Write, def: &Definitions) -> fmt::Result {
let mut first = true;
let mut submodules = self.iter_sub_modules().collect::<Vec<_>>();
submodules.sort_by(|(a, _), (b, _)| a.cmp(b));
for (submodule_name, submodule) in submodules {
if !first {
writer.write_str("\n\n")?;
}
first = false;
writeln!(writer, "module {submodule_name} {{")?;
submodule.write_definition(writer, def)?;
writer.write_str("}")?;
}
let mut vars = self.iter_var().collect::<Vec<_>>();
vars.sort_by(|(a, _), (b, _)| a.cmp(b));
for (name, value) in vars {
if !first {
writer.write_str("\n\n")?;
}
first = false;
let ty = def_type_name(value.type_name(), def.engine);
write!(writer, "const {name}: {ty};")?;
}
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.num_params.cmp(&b.metadata.num_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,
});
for f in func_infos {
if !first {
writer.write_str("\n\n")?;
}
first = false;
if f.metadata.access != FnAccess::Private {
let operator =
!f.metadata.name.contains('$') && !is_valid_function_name(&f.metadata.name);
#[cfg(not(feature = "no_custom_syntax"))]
let operator = operator || def.engine.is_custom_keyword(f.metadata.name.as_str());
f.write_definition(writer, def, operator)?;
}
}
Ok(())
}
}
impl FuncInfo {
fn write_definition(
&self,
writer: &mut dyn fmt::Write,
def: &Definitions,
operator: bool,
) -> fmt::Result {
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.num_params {
if !first {
writer.write_str(", ")?;
}
first = false;
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)),
)
});
if operator {
write!(writer, "{param_type}")?;
} else {
write!(writer, "{param_name}: {param_type}")?;
}
}
write!(
writer,
") -> {};",
def_type_name(&self.metadata.return_type, def.engine)
)?;
Ok(())
}
}
#[must_use]
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();
let ty = ty.split("::").last().unwrap();
let ty = ty
.strip_prefix("RhaiResultOf<")
.and_then(|s| s.strip_suffix('>'))
.map_or(ty, str::trim);
let ty = ty
.replace("Iterator<Item=", "Iterator<")
.replace("Dynamic", "?")
.replace("INT", "int")
.replace(type_name::<INT>(), "int")
.replace("FLOAT", "float")
.replace("&str", "String")
.replace("ImmutableString", "String");
#[cfg(not(feature = "no_float"))]
let ty = ty.replace(type_name::<crate::FLOAT>(), "float");
#[cfg(not(feature = "no_index"))]
let ty = ty.replace(type_name::<crate::Array>(), "Array");
#[cfg(not(feature = "no_index"))]
let ty = ty.replace(type_name::<crate::Blob>(), "Blob");
#[cfg(not(feature = "no_object"))]
let ty = ty.replace(type_name::<crate::Map>(), "Map");
#[cfg(not(feature = "no_time"))]
let ty = ty.replace(type_name::<crate::Instant>(), "Instant");
let ty = ty.replace(type_name::<FnPtr>(), "FnPtr");
ty.into()
}
impl Scope<'_> {
fn write_definition(&self, writer: &mut dyn fmt::Write, def: &Definitions) -> fmt::Result {
let mut first = true;
for (name, constant, value) in self.iter_raw() {
if !first {
writer.write_str("\n\n")?;
}
first = false;
let kw = if constant { Token::Const } else { Token::Let };
let ty = def_type_name(value.type_name(), def.engine);
write!(writer, "{kw} {name}: {ty};")?;
}
Ok(())
}
}