2020-12-20 12:27:47 +08:00
|
|
|
Generate Function Signatures
|
|
|
|
===========================
|
2020-11-23 20:27:20 +08:00
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
{{#include ../../links.md}}
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
`Engine::gen_fn_signatures`
|
|
|
|
--------------------------
|
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
As part of a _reflections_ API, `Engine::gen_fn_signatures` returns a list of function _signatures_
|
|
|
|
(as `Vec<String>`), each corresponding to a particular function available to that [`Engine`] instance.
|
2020-11-23 20:27:20 +08:00
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
> `fn_name ( param_1: type_1, param_2: type_2, ... , param_n : type_n ) -> return_type`
|
|
|
|
|
|
|
|
### Sources
|
2020-11-23 20:27:20 +08:00
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
Functions from the following sources are included, in order:
|
2020-11-23 20:27:20 +08:00
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
1) Native Rust functions registered into the global namespace via the `Engine::register_XXX` API
|
2020-12-22 23:45:14 +08:00
|
|
|
2) _Public_ (i.e. non-[`private`]) functions (native Rust or Rhai scripted) in global sub-modules
|
|
|
|
registered via `Engine::register_static_module`.
|
|
|
|
3) Native Rust functions in global modules registered via `Engine::register_global_module` (optional)
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
Functions Metadata
|
|
|
|
------------------
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
Beware, however, that not all function signatures contain parameters and return value information.
|
|
|
|
|
|
|
|
### `Engine::register_XXX`
|
|
|
|
|
|
|
|
For instance, functions registered via `Engine::register_XXX` contain no information on
|
|
|
|
the names of parameter and their actual types because Rust simply does not make such metadata
|
|
|
|
available natively. The return type is also undetermined.
|
|
|
|
|
2020-12-20 12:27:47 +08:00
|
|
|
A function registered under the name `foo` with three parameters and unknown return type:
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
> `foo(_, _, _)`
|
|
|
|
|
|
|
|
An operator function - again, unknown parameters and return type.
|
|
|
|
Notice that function names do not need to be valid identifiers.
|
|
|
|
|
|
|
|
> `+(_, _)`
|
|
|
|
|
|
|
|
A [property setter][getters/setters] - again, unknown parameters and return type.
|
|
|
|
Notice that function names do not need to be valid identifiers.
|
2020-12-20 12:27:47 +08:00
|
|
|
In this case, the first parameter should be `&mut T` of the custom type and the return value is `()`:
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
> `set$prop(_, _, _)`
|
|
|
|
|
2020-12-20 20:05:23 +08:00
|
|
|
### Script-Defined Functions
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
Script-defined [function] signatures contain parameter names. Since all parameters, as well as
|
|
|
|
the return value, are [`Dynamic`] the types are simply not shown.
|
|
|
|
|
|
|
|
A script-defined function always takes dynamic arguments, and the return type is also dynamic:
|
|
|
|
|
2020-12-19 17:46:34 +08:00
|
|
|
> `foo(x, y, z) -> Dynamic`
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
probably defined as:
|
|
|
|
|
|
|
|
```rust
|
|
|
|
fn foo(x, y, z) {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
is the same as:
|
|
|
|
|
|
|
|
> `foo(x: Dynamic, y: Dynamic, z: Dynamic) -> Result<Dynamic, Box<EvalAltResult>>`
|
|
|
|
|
2020-12-20 20:05:23 +08:00
|
|
|
### Plugin Functions
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
Functions defined in [plugin modules] are the best. They contain all the metadata
|
|
|
|
describing the functions.
|
|
|
|
|
2020-12-19 17:46:34 +08:00
|
|
|
For example, a plugin function `merge`:
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
> `merge(list: &mut MyStruct<i64>, num: usize, name: &str) -> Option<bool>`
|
|
|
|
|
|
|
|
Notice that function names do not need to be valid identifiers.
|
|
|
|
|
2020-12-19 17:46:34 +08:00
|
|
|
For example, an operator defined as a [fallible function] in a [plugin module] via
|
|
|
|
`#[rhai_fn(name="+=", return_raw)]` returns `Result<bool, Box<EvalAltResult>>`:
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
> `+=(list: &mut MyStruct<i64>, num: usize, name: &str) -> Result<bool, Box<EvalAltResult>>`
|
|
|
|
|
2020-12-19 17:46:34 +08:00
|
|
|
For example, a [property getter][getters/setters] defined in a [plugin module]:
|
2020-11-23 20:27:20 +08:00
|
|
|
|
|
|
|
> `get$prop(obj: &mut MyStruct<i64>) -> String`
|