#scripting #scripting-engine #scripting-language #embedded #plugin

macro no-std rhai_codegen

Procedural macros support package for Rhai, a scripting language and engine for Rust

20 releases (9 stable)

1.5.0 Dec 31, 2022
1.4.2 Jul 31, 2022
1.4.0 Mar 30, 2022
1.2.0 Nov 19, 2021
0.3.0 Nov 23, 2020

#186 in WebAssembly

Download history 8874/week @ 2022-10-15 8647/week @ 2022-10-22 8534/week @ 2022-10-29 10970/week @ 2022-11-05 8790/week @ 2022-11-12 7726/week @ 2022-11-19 10276/week @ 2022-11-26 9400/week @ 2022-12-03 9882/week @ 2022-12-10 9564/week @ 2022-12-17 5771/week @ 2022-12-24 9356/week @ 2022-12-31 9870/week @ 2023-01-07 13870/week @ 2023-01-14 11940/week @ 2023-01-21 11626/week @ 2023-01-28

49,184 downloads per month
Used in 85 crates (2 directly)

MIT/Apache

240KB
6K SLoC

Procedural Macros for Plugins

Rhai logo

This crate holds procedural macros for code generation, supporting the plugins system for Rhai.

This crate is automatically referenced by the Rhai crate. Normally it should not be used directly.


lib.rs:

This crate contains procedural macros to make creating Rhai plugin modules much easier.

Export an Entire Rust Module to a Rhai Module

use rhai::{EvalAltResult, FLOAT};
use rhai::plugin::*;
use rhai::module_resolvers::*;

#[export_module]
mod advanced_math {
    pub const MYSTIC_NUMBER: FLOAT = 42.0;

    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 = exported_module!(advanced_math);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced", m);
engine.set_module_resolver(r);

assert_eq!(engine.eval::<FLOAT>(
    r#"
        import "Math::Advanced" as math;
        math::euclidean_distance(0.0, 1.0, 0.0, math::MYSTIC_NUMBER)
    "#)?, 41.0);
#   Ok(())
# }

Register a Rust Function with a Rhai Module

use rhai::{EvalAltResult, FLOAT, Module};
use rhai::plugin::*;
use rhai::module_resolvers::*;

#[export_fn]
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.0 as FLOAT);
let mut m = Module::new();
set_exported_fn!(m, "euclidean_distance", distance_function);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced", m);
engine.set_module_resolver(r);

assert_eq!(engine.eval::<FLOAT>(
    r#"
        import "Math::Advanced" as math;
        math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number())
    "#)?, 41.0);
# Ok(())
# }

Register a Plugin Function with an Engine

use rhai::{EvalAltResult, FLOAT, Module};
use rhai::plugin::*;
use rhai::module_resolvers::*;

#[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 });
register_exported_fn!(engine, "euclidean_distance", distance_function);

assert_eq!(engine.eval::<FLOAT>(
        "euclidean_distance(0.0, 1.0, 0.0, get_mystic_number())"
    )?, 41.0);
# Ok(())
# }

Dependencies

~195–590KB
~14K SLoC