Refine docs.

This commit is contained in:
Stephen Chung
2020-12-26 23:21:16 +08:00
parent 66d3af256e
commit 88f63fa24b
16 changed files with 116 additions and 58 deletions

View File

@@ -7,22 +7,24 @@ Create a Module from an AST
`Module::eval_ast_as_new`
------------------------
A _module_ can be created from a single script (or pre-compiled [`AST`]) containing global variables,
A [module] can be created from a single script (or pre-compiled [`AST`]) containing global variables,
functions and sub-modules via the `Module::eval_ast_as_new` method.
See the section on [_Exporting Variables, Functions and Sub-Modules_][`export`] for details on how to prepare
a Rhai script for this purpose as well as to control which functions/variables to export.
See the section on [_Exporting Variables, Functions and Sub-Modules_][`export`] for details on how to
prepare a Rhai script for this purpose as well as to control which functions/variables to export.
When given an [`AST`], it is first evaluated, then the following items are exposed as members of the new module:
When given an [`AST`], it is first evaluated, then the following items are exposed as members of the
new [module]:
* Global variables - all variables exported via the `export` statement (those not exported remain hidden).
* Functions not specifically marked `private`.
* Global modules that remain in the [`Scope`] at the end of a script run.
* Global modules that remain in the [`Scope`] at the end of a script run (become sub-modules).
`Module::eval_ast_as_new` encapsulates the entire `AST` into each function call, merging the module namespace
with the global namespace. Therefore, functions defined within the same module script can cross-call each other.
`Module::eval_ast_as_new` encapsulates the entire `AST` into each function call, merging the
module namespace with the global namespace. Therefore, functions defined within the same module
script can cross-call each other.
Examples

View File

@@ -3,18 +3,19 @@ Override a Built-in Function
{{#include ../links.md}}
Any similarly-named function defined in a script overrides any built-in or registered
Any similarly-named function defined in a script _overrides_ any built-in or registered
native Rust function of the same name and number of parameters.
```rust
// Override the built-in function 'to_int'
fn to_int(num) {
print("Ha! Gotcha! " + num);
// Override the built-in function 'to_float' when called as a method
fn to_float() {
print("Ha! Gotcha! " + this);
42.0
}
let x = (123).to_int();
let x = 123.to_float();
print(x); // what happens?
print(x); // what happens?
```
A registered native Rust function, in turn, overrides any built-in function of the