...
This commit is contained in:
14
rhai_engine/rhaibook/lib/index.md
Normal file
14
rhai_engine/rhaibook/lib/index.md
Normal file
@@ -0,0 +1,14 @@
|
||||
External Packages
|
||||
=================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
Following are external [packages] that can be used with Rhai for additional functionalities.
|
||||
|
||||
| Package | Description |
|
||||
| :-----------: | --------------------------------------------------------------------- |
|
||||
| [`rhai-rand`] | generate random numbers, shuffling and sampling |
|
||||
| [`rhai-sci`] | functions for scientific computing |
|
||||
| [`rhai-ml`] | functions for AI and machine learning |
|
||||
| [`rhai-fs`] | read/write files in an external filesystem |
|
||||
| [`rhai-url`] | working with Urls via the [`url`](https://crates.io/crates/url) crate |
|
52
rhai_engine/rhaibook/lib/rhai-autodocs.md
Normal file
52
rhai_engine/rhaibook/lib/rhai-autodocs.md
Normal file
@@ -0,0 +1,52 @@
|
||||
`rhai-autodocs`: Generate API Documentation
|
||||
===========================================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
|
||||
`rhai-autodocs` helps generate API documentation, in [MarkDown] or [MDX] format, for functions
|
||||
registered inside an [`Engine`] instance.
|
||||
|
||||
It is typically imported as a build dependency into the build script.
|
||||
|
||||
The [MarkDown]/[MDX] files can then be used to create a static documentation site using generators
|
||||
such as [`mdbook`](https://crates.io/crates/mdbook) or [Docusaurus](https://docusaurus.io).
|
||||
|
||||
|
||||
> On `crates.io`: [`rhai-autodocs`](https://crates.io/crates/rhai-autodocs)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-autodocs`](https://github.com/ltabis/rhai-autodocs)
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dev-dependencies]
|
||||
rhai = "{{version}}"
|
||||
rhai-autodocs = "0.4" # use rhai-autodocs crate
|
||||
```
|
||||
|
||||
`build.rs`:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// Specify an environment variable that points to the directory
|
||||
// where the documentation will be generated.
|
||||
if let Ok(docs_path) = std::env::var("DOCS_DIR") {
|
||||
let mut engine = rhai::Engine::new();
|
||||
|
||||
// register custom functions and types...
|
||||
// or load packages...
|
||||
|
||||
let docs = rhai_autodocs::options()
|
||||
.include_standard_packages(false)
|
||||
.generate(&engine)
|
||||
.expect("failed to generate documentation");
|
||||
|
||||
// Write the documentation to a file etc.
|
||||
}
|
||||
}
|
||||
```
|
24
rhai_engine/rhaibook/lib/rhai-dylib.md
Normal file
24
rhai_engine/rhaibook/lib/rhai-dylib.md
Normal file
@@ -0,0 +1,24 @@
|
||||
Create Dynamically Loadable Rhai Libraries
|
||||
===========================================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
```admonish danger.small "Linux or Windows only"
|
||||
|
||||
`rhai-dylib` currently supports only Linux and Windows.
|
||||
```
|
||||
|
||||
`rhai-dylib` is an independent crate that demonstrates an API to register Rhai functionalities via
|
||||
_dynamic shared libraries_ (i.e. `.so` in Linux or `.dll` in Windows).
|
||||
|
||||
In other words, functions and [modules] can be defined in external libraries that are loaded
|
||||
dynamically at _runtime_, allowing for great flexibility at the cost of depending on the unstable
|
||||
Rust ABI.
|
||||
|
||||
A [module resolver] is also included.
|
||||
|
||||
> On `crates.io`: [`rhai-dylib`](https://crates.io/crates/rhai-dylib)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-dylib`](https://github.com/rhaiscript/rhai-dylib)
|
||||
>
|
||||
> API trait name: `rhai_dylib::Plugin`
|
83
rhai_engine/rhaibook/lib/rhai-fs.md
Normal file
83
rhai_engine/rhaibook/lib/rhai-fs.md
Normal file
@@ -0,0 +1,83 @@
|
||||
`rhai-fs`: Filesystem Access
|
||||
============================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
|
||||
`rhai-fs` is an independent Rhai [package] that enables reading from and writing to files in an
|
||||
external filesystem.
|
||||
|
||||
```admonish info.side "Documentation"
|
||||
|
||||
See <https://docs.rs/rhai-fs> for the list of functions.
|
||||
```
|
||||
|
||||
> On `crates.io`: [`rhai-fs`](https://crates.io/crates/rhai-fs)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-fs`](https://github.com/rhaiscript/rhai-fs)
|
||||
>
|
||||
> Package name: `FilesystemPackage`
|
||||
|
||||
|
||||
Dependency
|
||||
----------
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
rhai = "{{version}}"
|
||||
rhai-fs = "0.1" # use rhai-fs crate
|
||||
```
|
||||
|
||||
|
||||
Load Package into [`Engine`]
|
||||
----------------------------
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
use rhai::packages::Package; // needed for 'Package' trait
|
||||
use rhai_fs::FilesystemPackage;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Create new 'FilesystemPackage' instance
|
||||
let fs = FilesystemPackage::new();
|
||||
|
||||
// Load the package into the `Engine`
|
||||
fs.register_into_engine(&mut engine);
|
||||
```
|
||||
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
```js
|
||||
// Create a file, or open it if already exists
|
||||
let file = open_file("example.txt");
|
||||
|
||||
// Read the contents of the file (if any) into a BLOB
|
||||
let blob_buf = file.read_blob();
|
||||
|
||||
print(`file contents: ${blob_buf}`);
|
||||
|
||||
// Update BLOB data
|
||||
blob_buf.write_utf8(0..=0x20, "foobar");
|
||||
|
||||
print(`new file contents: ${blob_buf}`);
|
||||
|
||||
// Seek back to the beginning
|
||||
file.seek(0);
|
||||
|
||||
// Overwrite the original file with new data
|
||||
blob_buf.write_to_file(file);
|
||||
```
|
||||
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
| Feature | Description | Default? | Should be used with Rhai feature |
|
||||
| :--------: | ------------------------------------------------------------ | :------: | :------------------------------: |
|
||||
| `no_array` | removes support for [arrays] and [BLOB's] | **no** | [`no_index`] |
|
||||
| `metadata` | enables [functions metadata] (turns on [`metadata`] in Rhai) | **no** | |
|
57
rhai_engine/rhaibook/lib/rhai-ml.md
Normal file
57
rhai_engine/rhaibook/lib/rhai-ml.md
Normal file
@@ -0,0 +1,57 @@
|
||||
`rhai-ml`: Functions for AI and Machine Learning
|
||||
================================================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
|
||||
`rhai-ml` is an independent Rhai [package] that provides functions useful for
|
||||
artificial intelligence and machine learning.
|
||||
|
||||
```admonish info.side "Documentation"
|
||||
|
||||
See [https://docs.rs/rhai-ml](https://docs.rs/rhai-ml#api) for the list of functions.
|
||||
```
|
||||
|
||||
> On `crates.io`: [`rhai-ml`](https://crates.io/crates/rhai-ml)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-ml`](https://github.com/rhaiscript/rhai-ml)
|
||||
>
|
||||
> Package name: `MLPackage`
|
||||
|
||||
|
||||
Dependency
|
||||
----------
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
rhai = "{{version}}"
|
||||
rhai-ml = "0.1" # use rhai-ml crate
|
||||
```
|
||||
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
| Feature | Description | Default? |
|
||||
| :--------: | --------------------------------------------------------------------------------------------- | :------: |
|
||||
| `metadata` | enables [functions metadata] (turns on [`metadata`] in Rhai); necessary for running doc-tests | **no** |
|
||||
|
||||
|
||||
Load Package into [`Engine`]
|
||||
----------------------------
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
use rhai::packages::Package; // needed for 'Package' trait
|
||||
use rhai_ml::MLPackage;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Create new 'MLPackage' instance
|
||||
let ml = MLPackage::new();
|
||||
|
||||
// Load the package into the [`Engine`]
|
||||
ml.register_into_engine(&mut engine);
|
||||
```
|
74
rhai_engine/rhaibook/lib/rhai-rand.md
Normal file
74
rhai_engine/rhaibook/lib/rhai-rand.md
Normal file
@@ -0,0 +1,74 @@
|
||||
`rhai-rand`: Random Number Generation, Shuffling and Sampling
|
||||
=============================================================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
`rhai-rand` is an independent Rhai [package] that provides:
|
||||
|
||||
* random number generation using the [`rand`](https://crates.io/crates/rand) crate
|
||||
* [array] shuffling and sampling
|
||||
|
||||
```admonish info.side "Documentation"
|
||||
|
||||
See [https://docs.rs/rhai-rand](https://docs.rs/rhai-rand#api) for the list of functions.
|
||||
```
|
||||
|
||||
> On `crates.io`: [`rhai-rand`](https://crates.io/crates/rhai-rand)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-rand`](https://github.com/rhaiscript/rhai-rand)
|
||||
>
|
||||
> Package name: `RandomPackage`
|
||||
|
||||
|
||||
Dependency
|
||||
----------
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
rhai = "{{version}}"
|
||||
rhai-rand = "0.1" # use rhai-rand crate
|
||||
```
|
||||
|
||||
|
||||
Load Package into [`Engine`]
|
||||
----------------------------
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
use rhai::packages::Package; // needed for 'Package' trait
|
||||
use rhai_rand::RandomPackage;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Create new 'RandomPackage' instance
|
||||
let random = RandomPackage::new();
|
||||
|
||||
// Load the package into the `Engine`
|
||||
random.register_into_engine(&mut engine);
|
||||
```
|
||||
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
| Feature | Description | Default? | Should not be used with Rhai feature |
|
||||
| :--------: | ------------------------------------------------------------ | :------: | :----------------------------------: |
|
||||
| `float` | enables random floating-point number generation | yes | [`no_float`] |
|
||||
| `array` | enables methods for [arrays] | yes | [`no_index`] |
|
||||
| `metadata` | enables [functions metadata] (turns on [`metadata`] in Rhai) | **no** | |
|
||||
|
||||
~~~admonish example "Example: Working with `no_float` in Rhai"
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
# Rhai is set for 'no_float', meaning no floating-point support
|
||||
rhai = { version="{{version}}", features = ["no_float"] }
|
||||
|
||||
# Use 'default-features = false' to clear defaults, then only add 'array'
|
||||
rhai-rand = { version="0.1", default-features = false, features = ["array"] }
|
||||
```
|
||||
~~~
|
60
rhai_engine/rhaibook/lib/rhai-sci.md
Normal file
60
rhai_engine/rhaibook/lib/rhai-sci.md
Normal file
@@ -0,0 +1,60 @@
|
||||
`rhai-sci`: Functions for Scientific Computing
|
||||
==============================================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
|
||||
`rhai-sci` is an independent Rhai [package] that provides functions useful for
|
||||
scientific computing, inspired by languages like MATLAB, Octave, and R.
|
||||
|
||||
```admonish info.side "Documentation"
|
||||
|
||||
See [https://docs.rs/rhai-sci](https://docs.rs/rhai-sci#api) for the list of functions.
|
||||
```
|
||||
|
||||
> On `crates.io`: [`rhai-sci`](https://crates.io/crates/rhai-sci)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-sci`](https://github.com/rhaiscript/rhai-sci)
|
||||
>
|
||||
> Package name: `SciPackage`
|
||||
|
||||
|
||||
Dependency
|
||||
----------
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
rhai = "{{version}}"
|
||||
rhai-sci = "0.1" # use rhai-sci crate
|
||||
```
|
||||
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
| Feature | Description | Default? |
|
||||
| :--------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------: |
|
||||
| `metadata` | enables [functions metadata] (turns on [`metadata`] in Rhai); necessary for running doc-tests | **no** |
|
||||
| `io` | enables the `read_matrix` function but pulls in several additional dependencies | yes |
|
||||
| `nalgebra` | enables the functions `regress`, `inv`, `mtimes`, `horzcat`, `vertcat`, and `repmat` but pulls in [`nalgebra`](https://crates.io/crates/nalgebra) and [`linregress`](https://crates.io/crates/linregress). | yes |
|
||||
| `rand` | enables the `rand` function for generating random values and random matrices, but pulls in [`rand`](https://crates.io/crates/rand). | yes |
|
||||
|
||||
|
||||
Load Package into [`Engine`]
|
||||
----------------------------
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
use rhai::packages::Package; // needed for 'Package' trait
|
||||
use rhai_sci::SciPackage;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Create new 'SciPackage' instance
|
||||
let sci = SciPackage::new();
|
||||
|
||||
// Load the package into the [`Engine`]
|
||||
sci.register_into_engine(&mut engine);
|
||||
```
|
75
rhai_engine/rhaibook/lib/rhai-url.md
Normal file
75
rhai_engine/rhaibook/lib/rhai-url.md
Normal file
@@ -0,0 +1,75 @@
|
||||
`rhai-url`: Working with Urls
|
||||
=============================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
|
||||
`rhai-url` is an independent Rhai [package] that enables working with Urls via the
|
||||
[`url`](https://crates.io/crates/url) crate.
|
||||
|
||||
```admonish info.side "Documentation"
|
||||
|
||||
See <https://docs.rs/rhai-url> for the list of functions.
|
||||
```
|
||||
|
||||
> On `crates.io`: [`rhai-url`](https://crates.io/crates/rhai-url)
|
||||
>
|
||||
> On `GitHub`: [`rhaiscript/rhai-url`](https://github.com/rhaiscript/rhai-url)
|
||||
>
|
||||
> Package name: `FilesystemPackage`
|
||||
|
||||
|
||||
Dependency
|
||||
----------
|
||||
|
||||
`Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
rhai = "{{version}}"
|
||||
rhai-url = "0.0.1" # use rhai-url crate
|
||||
```
|
||||
|
||||
|
||||
Load Package into [`Engine`]
|
||||
----------------------------
|
||||
|
||||
```rust
|
||||
use rhai::Engine;
|
||||
use rhai::packages::Package; // needed for 'Package' trait
|
||||
use rhai_url::UrlPackage;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Create new 'UrlPackage' instance
|
||||
let url = UrlPackage::new();
|
||||
|
||||
// Load the package into the `Engine`
|
||||
url.register_into_engine(&mut engine);
|
||||
```
|
||||
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
```rust
|
||||
let url = Url("http://example.com/?q=query");
|
||||
|
||||
print(url); // prints 'http://example.com/?q=query'
|
||||
print(url.href); // prints 'http://example.com/?q=query'
|
||||
|
||||
print(url.query); // prints 'q=query'
|
||||
|
||||
// fragment and hash are aliases
|
||||
print(url.fragment); // prints ''
|
||||
print(url.hash); // prints ''
|
||||
|
||||
url.query_clear();
|
||||
|
||||
print(url.query); // prints ''
|
||||
|
||||
url.query_remove("q");
|
||||
url.query_append("q", "name");
|
||||
|
||||
print(url); // prints 'http://example.com/?q=name'
|
||||
```
|
Reference in New Issue
Block a user