-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #525 from schungx/master
Release 1.5.0.
- Loading branch information
Showing
52 changed files
with
1,529 additions
and
830 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,40 @@ | ||
Sample Applications | ||
=================== | ||
|
||
Sample applications that use the Rhai scripting engine. | ||
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 | | ||
|
||
How to Run | ||
---------- | ||
|
||
```bash | ||
cargo run --example sample_app_to_run | ||
Scriptable Event Handler With State Examples | ||
------------------------------------------- | ||
|
||
Because of its popularity, included are sample implementations for the pattern | ||
[_Scriptable Event Handler With State_](https://rhai.rs/book/patterns/events.html) in different styles. | ||
|
||
| Example | Handler Script | Description | | ||
| ------------------------------------------ | ------------------------------------------------------------------ | :---------------------------------------------------------: | | ||
| [`event_handler_main`](event_handler_main) | [`event_handler_main/script.rhai`](event_handler_main/script.rhai) | [_Main Style_](https://rhai.rs/book/patterns/events-1.html) | | ||
| [`event_handler_js`](event_handler_js) | [`event_handler_js/script.rhai`](event_handler_js/script.rhai) | [_JS Style_](https://rhai.rs/book/patterns/events-2.html) | | ||
| [`event_handler_map`](event_handler_map) | [`event_handler_map/script.rhai`](event_handler_map/script.rhai) | [_Map Style_](https://rhai.rs/book/patterns/events-3.html) | | ||
|
||
|
||
Running Examples | ||
---------------- | ||
|
||
Examples can be run with the following command: | ||
|
||
```sh | ||
cargo run --example {example_name} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! This example stores a Rhai closure for later use as a callback. | ||
|
||
use rhai::{Engine, EvalAltResult, FnPtr}; | ||
|
||
fn main() -> Result<(), Box<EvalAltResult>> { | ||
// This script creates a closure which may capture variables. | ||
let script = " | ||
let x = 20; | ||
// The following closure captures 'x' | ||
return |a, b| (x + a) * b; | ||
"; | ||
|
||
// To call a Rhai closure at a later time, you'd need three things: | ||
// 1) an `Engine` (with all needed functions registered), | ||
// 2) a compiled `AST`, | ||
// 3) the closure (of type `FnPtr`). | ||
let engine = Engine::new(); | ||
|
||
let ast = engine.compile(script)?; | ||
|
||
let closure = engine.eval_ast::<FnPtr>(&ast)?; | ||
|
||
// Create a closure that we can call any time, encapsulating the | ||
// `Engine`, `AST` and `FnPtr`. | ||
let func = move |x: i64, y: i64| -> Result<i64, _> { closure.call(&engine, &ast, (x, y)) }; | ||
|
||
// Now we can call `func` anywhere just like a normal function! | ||
let result = func(1, 2)?; | ||
|
||
println!("The Answer: {}", result); // prints 42 | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.