2020-06-20 06:06:17 +02:00
|
|
|
Sand-Boxing - Block Access to External Data
|
|
|
|
==========================================
|
|
|
|
|
|
|
|
{{#include ../links.md}}
|
|
|
|
|
|
|
|
Rhai is _sand-boxed_ so a script can never read from outside its own environment.
|
|
|
|
|
2020-08-07 12:40:31 +02:00
|
|
|
Furthermore, an [`Engine`] created non-`mut` cannot mutate any state, including itself
|
|
|
|
(and therefore it is also _re-entrant_).
|
2020-06-20 06:06:17 +02:00
|
|
|
|
2020-08-07 12:40:31 +02:00
|
|
|
It is highly recommended that [`Engine`]'s be created immutable as much as possible.
|
2020-06-20 06:06:17 +02:00
|
|
|
|
2020-08-07 12:40:31 +02:00
|
|
|
```rust
|
|
|
|
// Use the fluent API to configure an 'Engine' and then keep an immutable instance.
|
|
|
|
let engine = Engine::new()
|
|
|
|
.register_get("field", get_field)
|
|
|
|
.register_set("field", set_field)
|
|
|
|
.register_fn("do_work", action);
|
2020-06-20 06:06:17 +02:00
|
|
|
|
2020-08-07 12:40:31 +02:00
|
|
|
// 'engine' is immutable...
|
2020-06-20 06:06:17 +02:00
|
|
|
```
|
2020-08-07 12:40:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
Using Rhai to Control External Environment
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
How does a _sand-boxed_, immutable [`Engine`] control the external environment?
|
|
|
|
This is necessary in order to use Rhai as a _dynamic control layer_ over a Rust core system.
|
|
|
|
|
|
|
|
There are two general patterns, both involving wrapping the external system
|
|
|
|
in a shared, interior-mutated object (e.g. `Rc<RefCell<T>>`):
|
|
|
|
|
|
|
|
* [Control Layer]({{rootUrl}}/patterns/control.md) pattern.
|
|
|
|
|
|
|
|
* [Singleton Command Object]({{rootUrl}}/patterns/singleton.md) pattern.
|