Fix definitions API code styles and formatting.

This commit is contained in:
Stephen Chung
2022-07-26 22:38:40 +08:00
parent a891b54d0f
commit 8e21c4727b
10 changed files with 314 additions and 108 deletions

View File

@@ -4,18 +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 |
| [`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 |
| 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 |
| [`definitions`](./definitions) | shows how to generate definition files for use with the [Rhai Language Server](https://github.com/rhaiscript/lsp) (requires the `metadata` feature) |
| [`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 |
Scriptable Event Handler With State Examples

View File

@@ -249,3 +249,11 @@ op +=(Blob, Blob);
op +=(Blob, i64);
op +=(Blob, char);
op +=(Blob, String);
op in(?, Array) -> bool;
op in(String, String) -> bool;
op in(char, String) -> bool;
op in(int, Range<int>) -> bool;
op in(int, RangeInclusive<int>) -> bool;
op in(String, Map) -> bool;
op in(int, Blob) -> bool;

View File

@@ -112,7 +112,7 @@ fn curry(fn_ptr: FnPtr, ...args: ?) -> FnPtr;
/// print(is_def_fn("foo", 0)); // prints false
/// print(is_def_fn("bar", 1)); // prints false
/// ```
fn is_def_fn(fn_name: String, num_params: i64) -> bool;
fn is_def_fn(fn_name: String, num_params: int) -> bool;
/// Return `true` if a variable matching a specified name is defined.
///
@@ -162,9 +162,100 @@ fn is_shared(variable: ?) -> bool;
/// ```
fn eval(script: String) -> ?;
/// Return `true` if the string contains another string.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let x = "hello world!";
///
/// // The 'in' operator calls 'contains' in the background
/// if "world" in x {
/// print("found!");
/// }
/// ```
fn contains(string: String, find: String) -> bool;
fn contains(range: Range<i64>, value: i64) -> bool;
fn contains(range: RangeInclusive<i64>, value: i64) -> bool;
fn contains(map: Map, string: String) -> bool;
fn contains(blob: Blob, value: i64) -> bool;
/// Return `true` if the string contains a character.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let x = "hello world!";
///
/// // The 'in' operator calls 'contains' in the background
/// if 'w' in x {
/// print("found!");
/// }
/// ```
fn contains(string: String, ch: char) -> bool;
/// Return `true` if a value falls within the exclusive range.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let r = 1..100;
///
/// // The 'in' operator calls 'contains' in the background
/// if 42 in r {
/// print("found!");
/// }
/// ```
fn contains(range: Range<int>, value: int) -> bool;
/// Return `true` if a value falls within the inclusive range.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let r = 1..=100;
///
/// // The 'in' operator calls 'contains' in the background
/// if 42 in r {
/// print("found!");
/// }
/// ```
fn contains(range: RangeInclusive<int>, value: int) -> bool;
/// Return `true` if a key exists within the object map.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let m = #{a:1, b:2, c:3};
///
/// // The 'in' operator calls 'contains' in the background
/// if "c" in m {
/// print("found!");
/// }
/// ```
fn contains(map: Map, string: String) -> bool;
/// Return `true` if a value is found within the BLOB.
///
/// This function also drives the `in` operator.
///
/// # Example
///
/// ```rhai
/// let b = blob();
///
/// b += 1; b += 2; b += 3; b += 4; b += 5;
///
/// // The 'in' operator calls 'contains' in the background
/// if 3 in b {
/// print("found!");
/// }
/// ```
fn contains(blob: Blob, value: int) -> bool;

View File

@@ -1,5 +1,4 @@
module general_kenobi;
/// Returns a string where `hello there `
/// is repeated `n` times.
/// Returns a string where "hello there" is repeated `n` times.
fn hello_there(n: i64) -> String;

View File

@@ -1,21 +1,20 @@
use rhai::{plugin::*, Engine, Scope};
use rhai::plugin::*;
use rhai::{Engine, EvalAltResult, Scope};
#[export_module]
pub mod general_kenobi {
/// Returns a string where `hello there `
/// is repeated `n` times.
/// 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() {
fn main() -> Result<(), Box<EvalAltResult>> {
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.
// This variable will also show up in the definitions, since it will be part of the scope.
scope.push("hello_there", "hello there");
#[cfg(not(feature = "no_module"))]
@@ -28,17 +27,15 @@ fn main() {
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.run_with_scope(
&mut scope,
"hello_there = general_kenobi::hello_there(4 minus 2);",
)?;
engine
.definitions_with_scope(&scope)
.write_to_dir("examples/definitions/.rhai/definitions")
.unwrap();
Ok(())
}