feat: basic definitions
This commit is contained in:
@@ -4,17 +4,18 @@ Sample Applications
|
||||
Standard Examples
|
||||
-----------------
|
||||
|
||||
| Example | Description |
|
||||
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`arrays_and_structs`](arrays_and_structs.rs) | shows how to register a Rust type and using it with arrays |
|
||||
| [`callback`](callback.rs) | shows how to store a Rhai closure and call it later within Rust |
|
||||
| [`custom_types_and_methods`](custom_types_and_methods.rs) | shows how to register a Rust type and methods/getters/setters for it |
|
||||
| [`hello`](hello.rs) | simple example that evaluates an expression and prints the result |
|
||||
| [`reuse_scope`](reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common `Scope` |
|
||||
| [`serde`](serde.rs) | example to serialize and deserialize Rust types with [`serde`](https://crates.io/crates/serde) (requires the `serde` feature) |
|
||||
| [`simple_fn`](simple_fn.rs) | shows how to register a simple Rust function |
|
||||
| [`strings`](strings.rs) | shows different ways to register Rust functions taking string arguments |
|
||||
| [`threading`](threading.rs) | shows how to communicate with an `Engine` running in a separate thread via an MPSC channel |
|
||||
| Example | Description |
|
||||
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| [`arrays_and_structs`](arrays_and_structs.rs) | shows how to register a Rust type and using it with arrays |
|
||||
| [`callback`](callback.rs) | shows how to store a Rhai closure and call it later within Rust |
|
||||
| [`custom_types_and_methods`](custom_types_and_methods.rs) | shows how to register a Rust type and methods/getters/setters for it |
|
||||
| [`hello`](hello.rs) | simple example that evaluates an expression and prints the result |
|
||||
| [`reuse_scope`](reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common `Scope` |
|
||||
| [`serde`](serde.rs) | example to serialize and deserialize Rust types with [`serde`](https://crates.io/crates/serde) (requires the `serde` feature) |
|
||||
| [`simple_fn`](simple_fn.rs) | shows how to register a simple Rust function |
|
||||
| [`strings`](strings.rs) | shows different ways to register Rust functions taking string arguments |
|
||||
| [`threading`](threading.rs) | shows how to communicate with an `Engine` running in a separate thread via an MPSC channel |
|
||||
| [`definitions`](./definitions) | shows how to generate definition files for manual inspection or for use with [Rhai LSP](https://github.com/rhaiscript/lsp), requires the `metadata` feature |
|
||||
|
||||
|
||||
Scriptable Event Handler With State Examples
|
||||
|
3
examples/definitions/.rhai/definitions/__scope__.d.rhai
Normal file
3
examples/definitions/.rhai/definitions/__scope__.d.rhai
Normal file
@@ -0,0 +1,3 @@
|
||||
module static;
|
||||
|
||||
let hello_there;
|
5731
examples/definitions/.rhai/definitions/__static__.d.rhai
Normal file
5731
examples/definitions/.rhai/definitions/__static__.d.rhai
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,5 @@
|
||||
module general_kenobi;
|
||||
|
||||
/// Returns a string where `hello there `
|
||||
/// is repeated `n` times.
|
||||
fn hello_there(n: i64) -> String;
|
40
examples/definitions/main.rs
Normal file
40
examples/definitions/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use rhai::{plugin::*, Engine, Scope};
|
||||
|
||||
#[export_module]
|
||||
pub mod general_kenobi {
|
||||
/// Returns a string where `hello there `
|
||||
/// is repeated `n` times.
|
||||
pub fn hello_there(n: i64) -> String {
|
||||
use std::convert::TryInto;
|
||||
"hello there ".repeat(n.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
|
||||
// This variable will also show up in the definitions,
|
||||
// since it will be part of the scope.
|
||||
scope.push("hello_there", "hello there");
|
||||
|
||||
engine.register_static_module("general_kenobi", exported_module!(general_kenobi).into());
|
||||
|
||||
// Custom operators also show up in definitions.
|
||||
engine.register_custom_operator("minus", 100).unwrap();
|
||||
engine.register_fn("minus", |a: i64, b: i64| a - b);
|
||||
|
||||
engine
|
||||
.eval_with_scope::<()>(
|
||||
&mut scope,
|
||||
r#"
|
||||
hello_there = general_kenobi::hello_there(4 minus 2);
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
engine
|
||||
.definitions_with_scope(&scope)
|
||||
.write_to_dir("examples/definitions/.rhai/definitions")
|
||||
.unwrap();
|
||||
}
|
3
examples/definitions/script.rhai
Normal file
3
examples/definitions/script.rhai
Normal file
@@ -0,0 +1,3 @@
|
||||
// The following will be valid based on the definitions.
|
||||
hello_there = general_kenobi::hello_there(123);
|
||||
print(hello_there);
|
Reference in New Issue
Block a user