2022-07-26 16:38:40 +02:00
|
|
|
use rhai::plugin::*;
|
|
|
|
use rhai::{Engine, EvalAltResult, Scope};
|
2022-07-25 19:01:06 +02:00
|
|
|
|
|
|
|
#[export_module]
|
|
|
|
pub mod general_kenobi {
|
2022-09-08 04:53:55 +02:00
|
|
|
/// General Kenobi's Constant.
|
|
|
|
pub const CONSTANT: i64 = 42;
|
|
|
|
|
2022-07-26 16:38:40 +02:00
|
|
|
/// Returns a string where "hello there" is repeated `n` times.
|
2022-07-25 19:01:06 +02:00
|
|
|
pub fn hello_there(n: i64) -> String {
|
|
|
|
use std::convert::TryInto;
|
|
|
|
"hello there ".repeat(n.try_into().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:38:40 +02:00
|
|
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
2022-07-25 19:01:06 +02:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
2022-07-26 16:38:40 +02:00
|
|
|
// This variable will also show up in the definitions, since it will be part of the scope.
|
2022-07-25 19:01:06 +02:00
|
|
|
scope.push("hello_there", "hello there");
|
|
|
|
|
2022-09-08 04:53:55 +02:00
|
|
|
// This constant will also show up in the definitions, since it will be part of the scope.
|
|
|
|
scope.push_constant("HELLO", "hello there");
|
|
|
|
|
2022-07-26 14:28:54 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-07-25 19:01:06 +02:00
|
|
|
engine.register_static_module("general_kenobi", exported_module!(general_kenobi).into());
|
|
|
|
|
|
|
|
// Custom operators also show up in definitions.
|
2022-07-26 14:28:54 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
|
|
|
{
|
|
|
|
engine.register_custom_operator("minus", 100).unwrap();
|
|
|
|
engine.register_fn("minus", |a: i64, b: i64| a - b);
|
|
|
|
}
|
2022-07-25 19:01:06 +02:00
|
|
|
|
2022-07-26 16:38:40 +02:00
|
|
|
engine.run_with_scope(
|
|
|
|
&mut scope,
|
|
|
|
"hello_there = general_kenobi::hello_there(4 minus 2);",
|
|
|
|
)?;
|
2022-07-25 19:01:06 +02:00
|
|
|
|
2022-07-27 23:39:03 +02:00
|
|
|
// Generate definitions for the contents of the engine and the scope.
|
2022-07-25 19:01:06 +02:00
|
|
|
engine
|
|
|
|
.definitions_with_scope(&scope)
|
|
|
|
.write_to_dir("examples/definitions/.rhai/definitions")
|
|
|
|
.unwrap();
|
2022-07-26 16:38:40 +02:00
|
|
|
|
2022-07-27 23:39:03 +02:00
|
|
|
// Alternatively we can write all of the above to a single file.
|
|
|
|
engine
|
|
|
|
.definitions_with_scope(&scope)
|
|
|
|
.write_to_file("examples/definitions/.rhai/all_in_one.d.rhai")
|
|
|
|
.unwrap();
|
|
|
|
|
2022-07-29 07:39:51 +02:00
|
|
|
// Skip standard packages if not needed (e.g. they are provided elsewhere).
|
|
|
|
engine
|
|
|
|
.definitions_with_scope(&scope)
|
|
|
|
.include_standard_packages(false)
|
|
|
|
.write_to_file("examples/definitions/.rhai/all_in_one_without_standard.d.rhai")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Write function definitions as JSON.
|
|
|
|
let json = engine
|
|
|
|
.definitions()
|
|
|
|
.include_standard_packages(false)
|
|
|
|
.json()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
std::fs::write("examples/definitions/.rhai/defs.json", json).unwrap();
|
|
|
|
|
2022-07-26 16:38:40 +02:00
|
|
|
Ok(())
|
2022-07-25 19:01:06 +02:00
|
|
|
}
|