rhai/codegen/src/lib.rs

351 lines
10 KiB
Rust
Raw Normal View History

//! This crate contains procedural macros to make creating Rhai plugin-modules much easier.
2020-08-01 18:52:26 +02:00
//!
2020-09-30 17:27:41 +02:00
//! # Export an Entire Rust Module to a Rhai `Module`
2020-08-01 18:52:26 +02:00
//!
//! ```
2020-08-03 02:27:19 +02:00
//! use rhai::{EvalAltResult, FLOAT};
2020-08-01 18:52:26 +02:00
//! use rhai::plugin::*;
//! use rhai::module_resolvers::*;
//!
//! #[export_module]
//! mod advanced_math {
2020-08-01 18:52:26 +02:00
//! pub const MYSTIC_NUMBER: FLOAT = 42.0 as FLOAT;
//!
//! pub fn euclidean_distance(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
//! ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt()
//! }
//! }
//!
//! fn main() -> Result<(), Box<EvalAltResult>> {
//! let mut engine = Engine::new();
//! let m = rhai::exported_module!(advanced_math);
//! let mut r = StaticModuleResolver::new();
//! r.insert("Math::Advanced".to_string(), m);
//! engine.set_module_resolver(Some(r));
//!
//! assert_eq!(engine.eval::<FLOAT>(
//! r#"import "Math::Advanced" as math;
//! let m = math::MYSTIC_NUMBER;
//! let x = math::euclidean_distance(0.0, 1.0, 0.0, m);
//! x"#)?, 41.0);
//! Ok(())
//! }
//! ```
//!
2020-09-30 17:27:41 +02:00
//! # Register a Rust Function with a Rhai `Module`
2020-08-01 18:52:26 +02:00
//!
//! ```
//! use rhai::{EvalAltResult, FLOAT, Module, RegisterFn};
//! use rhai::plugin::*;
//! use rhai::module_resolvers::*;
//!
//! #[export_fn]
//! fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
2020-08-01 18:52:26 +02:00
//! ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt()
//! }
//!
//! fn main() -> Result<(), Box<EvalAltResult>> {
//!
//! let mut engine = Engine::new();
//! engine.register_fn("get_mystic_number", || { 42 as FLOAT });
//! let mut m = Module::new();
//! rhai::set_exported_fn!(m, "euclidean_distance", distance_function);
2020-08-01 18:52:26 +02:00
//! let mut r = StaticModuleResolver::new();
//! r.insert("Math::Advanced".to_string(), m);
//! engine.set_module_resolver(Some(r));
//!
//! assert_eq!(engine.eval::<FLOAT>(
//! r#"import "Math::Advanced" as math;
//! let m = get_mystic_number();
//! let x = math::euclidean_distance(0.0, 1.0, 0.0, m);
//! x"#)?, 41.0);
//! Ok(())
//! }
//! ```
//!
2020-09-30 17:27:41 +02:00
//! # Register a Plugin Function with an `Engine`
//!
//! ```
//! use rhai::{EvalAltResult, FLOAT, Module, RegisterFn};
//! use rhai::plugin::*;
//! use rhai::module_resolvers::*;
//!
//! #[rhai::export_fn]
//! pub fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
//! ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt()
//! }
//!
//! fn main() -> Result<(), Box<EvalAltResult>> {
//!
//! let mut engine = Engine::new();
//! engine.register_fn("get_mystic_number", || { 42 as FLOAT });
//! rhai::register_exported_fn!(engine, "euclidean_distance", distance_function);
//!
//! assert_eq!(engine.eval::<FLOAT>(
//! r#"let m = get_mystic_number();
//! let x = euclidean_distance(0.0, 1.0, 0.0, m);
//! x"#)?, 41.0);
//! Ok(())
//! }
//! ```
//!
2020-08-01 18:52:26 +02:00
use quote::quote;
use syn::parse_macro_input;
2020-08-01 18:52:26 +02:00
2020-08-24 00:53:30 +02:00
mod attrs;
2020-08-01 18:52:26 +02:00
mod function;
mod module;
mod register;
2020-08-01 18:52:26 +02:00
mod rhai_module;
#[cfg(test)]
mod test;
/// Attribute, when put on a Rust function, turns it into a _plugin function_.
///
/// # Usage
///
2020-10-07 16:51:43 +02:00
/// ```
/// # use rhai::{Engine, EvalAltResult};
/// use rhai::plugin::*;
///
/// #[export_fn]
2020-10-07 16:51:43 +02:00
/// fn my_plugin_function(x: i64) -> i64 {
/// x * 2
/// }
2020-10-07 16:51:43 +02:00
///
/// # fn main() -> Result<(), Box<EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// register_exported_fn!(engine, "func", my_plugin_function);
///
/// assert_eq!(engine.eval::<i64>("func(21)")?, 42);
/// # Ok(())
/// # }
/// ```
2020-08-01 18:52:26 +02:00
#[proc_macro_attribute]
2020-08-02 09:39:08 +02:00
pub fn export_fn(
2020-08-08 04:19:17 +02:00
args: proc_macro::TokenStream,
2020-08-02 09:39:08 +02:00
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
2020-08-16 12:24:42 +02:00
let mut output = proc_macro2::TokenStream::from(input.clone());
2020-08-08 16:31:15 +02:00
2020-08-24 00:53:30 +02:00
let parsed_params = match crate::attrs::outer_item_attributes(args.into(), "export_fn") {
Ok(args) => args,
Err(err) => return proc_macro::TokenStream::from(err.to_compile_error()),
};
let mut function_def = parse_macro_input!(input as function::ExportedFn);
if let Err(e) = function_def.set_params(parsed_params) {
return e.to_compile_error().into();
}
2020-08-08 16:31:15 +02:00
output.extend(function_def.generate());
2020-08-01 18:52:26 +02:00
proc_macro::TokenStream::from(output)
}
/// Attribute, when put on a Rust module, turns it into a _plugin module_.
///
/// # Usage
///
2020-10-07 16:51:43 +02:00
/// ```
/// # use rhai::{Engine, Module, EvalAltResult};
/// use rhai::plugin::*;
///
/// #[export_module]
/// mod my_plugin_module {
2020-10-07 16:51:43 +02:00
/// pub fn foo(x: i64) -> i64 { x * 2 }
/// pub fn bar() -> i64 { 21 }
/// }
2020-10-07 16:51:43 +02:00
///
/// # fn main() -> Result<(), Box<EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// let module = exported_module!(my_plugin_module);
///
/// engine.load_package(module);
///
/// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42);
/// # Ok(())
/// # }
/// ```
2020-08-01 18:52:26 +02:00
#[proc_macro_attribute]
2020-08-02 09:39:08 +02:00
pub fn export_module(
args: proc_macro::TokenStream,
2020-08-02 09:39:08 +02:00
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let parsed_params = match crate::attrs::outer_item_attributes(args.into(), "export_module") {
Ok(args) => args,
Err(err) => return proc_macro::TokenStream::from(err.to_compile_error()),
};
let mut module_def = parse_macro_input!(input as module::Module);
if let Err(e) = module_def.set_params(parsed_params) {
return e.to_compile_error().into();
}
2020-08-01 18:52:26 +02:00
let tokens = module_def.generate();
proc_macro::TokenStream::from(tokens)
}
2020-10-07 16:51:43 +02:00
/// Macro to generate a Rhai `Module` from a _plugin module_ defined via `#[export_module]`.
///
/// # Usage
///
2020-10-07 16:51:43 +02:00
/// ```
/// # use rhai::{Engine, Module, EvalAltResult};
/// use rhai::plugin::*;
///
/// #[export_module]
/// mod my_plugin_module {
2020-10-07 16:51:43 +02:00
/// pub fn foo(x: i64) -> i64 { x * 2 }
/// pub fn bar() -> i64 { 21 }
/// }
///
2020-10-07 16:51:43 +02:00
/// # fn main() -> Result<(), Box<EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// let module = exported_module!(my_plugin_module);
2020-10-07 16:51:43 +02:00
///
/// engine.load_package(module);
///
/// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42);
/// # Ok(())
/// # }
/// ```
2020-08-01 18:52:26 +02:00
#[proc_macro]
pub fn exported_module(module_path: proc_macro::TokenStream) -> proc_macro::TokenStream {
let module_path = parse_macro_input!(module_path as syn::Path);
let tokens = quote::quote! {
#module_path::rhai_module_generate()
2020-08-01 18:52:26 +02:00
};
proc_macro::TokenStream::from(tokens)
}
/// Macro to combine a _plugin module_ into an existing module.
///
/// Functions and variables in the plugin module overrides any existing similarly-named
/// functions and variables in the target module.
///
/// This call is intended to be used within the `def_package!` macro to define a custom
/// package based on a plugin module.
///
/// All sub-modules, if any, in the plugin module are _flattened_ and their functions/variables
/// registered at the top level because packages require so.
///
/// The text string name in the second parameter can be anything and is reserved for future use;
/// it is recommended to be an ID string that uniquely identifies the plugin module.
///
/// # Usage
///
2020-10-07 16:51:43 +02:00
/// ```
/// # use rhai::{Engine, Module, EvalAltResult};
/// use rhai::plugin::*;
///
/// #[export_module]
/// mod my_plugin_module {
2020-10-07 16:51:43 +02:00
/// pub fn foo(x: i64) -> i64 { x * 2 }
/// pub fn bar() -> i64 { 21 }
/// }
///
2020-10-07 16:51:43 +02:00
/// # fn main() -> Result<(), Box<EvalAltResult>> {
/// let mut engine = Engine::new();
///
2020-10-07 16:51:43 +02:00
/// let mut module = Module::new();
/// combine_with_exported_module!(&mut module, "my_plugin_module_ID", my_plugin_module);
2020-10-07 16:51:43 +02:00
///
/// engine.load_package(module);
///
/// assert_eq!(engine.eval::<i64>("foo(bar())")?, 42);
/// # Ok(())
/// # }
/// ```
2020-09-13 16:12:11 +02:00
#[proc_macro]
pub fn combine_with_exported_module(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
2020-09-17 05:40:31 +02:00
let (module_expr, _export_name, module_path) = match crate::register::parse_register_macro(args)
2020-09-13 16:12:11 +02:00
{
Ok(triple) => triple,
Err(e) => return e.to_compile_error().into(),
};
let tokens = quote! {
2020-09-14 05:40:15 +02:00
#module_path::rhai_generate_into_module(#module_expr, true);
2020-09-13 16:12:11 +02:00
};
proc_macro::TokenStream::from(tokens)
}
2020-10-07 16:51:43 +02:00
/// Macro to register a _plugin function_ (defined via `#[export_fn]`) into an `Engine`.
///
/// # Usage
///
2020-10-07 16:51:43 +02:00
/// ```
/// # use rhai::{Engine, EvalAltResult};
/// use rhai::plugin::*;
///
/// #[export_fn]
2020-10-07 16:51:43 +02:00
/// fn my_plugin_function(x: i64) -> i64 {
/// x * 2
/// }
///
2020-10-07 16:51:43 +02:00
/// # fn main() -> Result<(), Box<EvalAltResult>> {
/// let mut engine = Engine::new();
///
2020-10-07 16:51:43 +02:00
/// register_exported_fn!(engine, "func", my_plugin_function);
///
/// assert_eq!(engine.eval::<i64>("func(21)")?, 42);
/// # Ok(())
/// # }
/// ```
2020-08-01 18:52:26 +02:00
#[proc_macro]
pub fn register_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
let (engine_expr, export_name, rust_modpath) = match crate::register::parse_register_macro(args)
{
Ok(triple) => triple,
Err(e) => return e.to_compile_error().into(),
2020-08-01 18:52:26 +02:00
};
let gen_mod_path = crate::register::generated_module_path(&rust_modpath);
let tokens = quote! {
#engine_expr.register_result_fn(&(#export_name), #gen_mod_path::dynamic_result_fn);
2020-08-01 18:52:26 +02:00
};
proc_macro::TokenStream::from(tokens)
}
/// Macro to register a _plugin function_ into a Rhai `Module`.
///
/// # Usage
///
2020-10-07 16:51:43 +02:00
/// ```
/// # use rhai::{Engine, EvalAltResult};
/// use rhai::plugin::*;
///
/// #[export_fn]
2020-10-07 16:51:43 +02:00
/// fn my_plugin_function(x: i64) -> i64 {
/// x * 2
/// }
///
2020-10-07 16:51:43 +02:00
/// # fn main() -> Result<(), Box<EvalAltResult>> {
/// let mut engine = Engine::new();
///
/// let mut module = Module::new();
2020-10-07 16:51:43 +02:00
/// set_exported_fn!(module, "func", my_plugin_function);
///
/// engine.load_package(module);
///
2020-10-07 16:51:43 +02:00
/// assert_eq!(engine.eval::<i64>("func(21)")?, 42);
/// # Ok(())
/// # }
/// ```
#[proc_macro]
pub fn set_exported_fn(args: proc_macro::TokenStream) -> proc_macro::TokenStream {
let (module_expr, export_name, rust_modpath) = match crate::register::parse_register_macro(args)
{
Ok(triple) => triple,
Err(e) => return e.to_compile_error().into(),
2020-08-01 18:52:26 +02:00
};
let gen_mod_path = crate::register::generated_module_path(&rust_modpath);
2020-08-01 18:52:26 +02:00
let tokens = quote! {
#module_expr.set_fn(#export_name, FnAccess::Public,
#gen_mod_path::token_input_types().as_ref(),
#gen_mod_path::token_callable());
2020-08-01 18:52:26 +02:00
};
proc_macro::TokenStream::from(tokens)
}