Rhai 脚本引擎
用户指南
1.
介绍
❱
1.1.
Features of Rhai
1.2.
Supported Targets and Builds
1.3.
What Rhai Isn't
1.4.
Licensing
1.5.
Related Resources
2.
Getting Started
❱
2.1.
Online Playground
2.2.
Install the Rhai Crate
2.3.
Optional Features
2.4.
Packaged Utilities
3.
The Scripting Engine
❱
3.1.
“Hello, Rhai”
3.2.
Compile to AST
3.3.
Raw Engine
❱
3.3.1.
Built-in Operators
3.4.
Scope – Maintaining State
3.5.
Expressions Only
3.6.
Options
4.
Examples
❱
4.1.
Rust
4.2.
Scripts
5.
Special Builds
❱
5.1.
Performance
5.2.
Minimal
5.3.
no-std
5.4.
WebAssembly (WASM)
Rust Integration
6.
Introduction
7.
Traits
8.
Register a Rust Function
❱
8.1.
String Parameters
8.2.
Generic Functions
8.3.
Fallible Functions
8.4.
Dynamic Parameters
8.5.
NativeCallContext
8.6.
Override a Built-in Function
9.
Call a Rhai Function from Rust
❱
9.1.
Create a Rust Closure from a Rhai Function
10.
Operator Overloading
11.
Working with Any Rust Type
❱
11.1.
Methods
11.2.
Property Getters and Setters
11.3.
Indexers
11.4.
Call Method as Function
11.5.
Collection Types
11.6.
Disable Custom Types
11.7.
Printing Custom Types
12.
Modules
❱
12.1.
Create in Rust
12.2.
Create from AST
12.3.
Use a Module
12.4.
Module Resolvers
❱
12.4.1.
Custom Module Resolvers
12.5.
Self-Contained AST
13.
Plugins
❱
13.1.
Export a Rust Module
13.2.
Export a Rust Function
14.
Packages
❱
14.1.
Built-in Packages
14.2.
Create Custom Packages
14.3.
Create Packages as Crates
14.4.
External Packages
❱
14.4.1.
Random Number Generation, Shuffling and Sampling
14.4.2.
Scientific Computing
Scripting Language
15.
Comments
❱
15.1.
Doc-Comments
16.
Values and Types
❱
16.1.
Dynamic Values
❱
16.1.1.
type_of()
16.1.2.
Value Tag
16.1.3.
Serialization/Deserialization with serde
16.2.
Numbers
❱
16.2.1.
Operators
16.2.2.
Standard Functions
16.2.3.
Value Conversions
16.2.4.
Ranges
16.2.5.
Bit-Fields
16.3.
Strings and Characters
❱
16.3.1.
ImmutableString
16.3.2.
Standard Functions and Operators
16.4.
Arrays
❱
16.4.1.
BLOB's (Byte Arrays)
16.5.
Object Maps
❱
16.5.1.
Parse from JSON
16.5.2.
Special Support for OOP
16.6.
Timestamps
17.
Keywords
18.
Statements
❱
18.1.
Statement Expression
19.
Variables
❱
19.1.
Variable Shadowing
19.2.
Strict Variables Mode
19.3.
Variable Definition Filter
19.4.
Variable Resolver
20.
Constants
❱
20.1.
Automatic Global Module
21.
Assignments
22.
Compound Assignments
23.
Logic Operators
24.
In Operator
25.
If Statement
❱
25.1.
If Expression
26.
Switch Statement
❱
26.1.
Switch Expression
27.
While Loop
28.
Do Loop
29.
Loop Statement
30.
For Loop
❱
30.1.
Make a Custom Type Iterable
31.
Return Value
32.
Throw Exception on Error
33.
Catch Exceptions
34.
Functions
❱
34.1.
Method Calls
34.2.
Overloading
34.3.
Namespaces
34.4.
Function Pointers
❱
34.4.1.
Currying
34.4.2.
Anonymous Functions
34.4.3.
Closures
34.5.
Metadata
❱
34.5.1.
Get Scripted Functions Metadata in Rhai
34.5.2.
Get Scripted Functions Metadata from AST
34.5.3.
Get Native Function Signatures
34.5.4.
Export All Functions Metadata to JSON
35.
Print and Debug
36.
Modules
❱
36.1.
Export Variables, Functions and Sub-Modules from Script
36.2.
Import Modules
37.
Eval Function
Safety and Protection
38.
Introduction
39.
Sand-Boxing
40.
Maximum Length of Strings
41.
Maximum Size of Arrays
42.
Maximum Size of Object Maps
43.
Maximum Number of Operations
❱
43.1.
Tracking Progress and Force-Termination
44.
Maximum Number of Modules
45.
Maximum Call Stack Depth
46.
Maximum Expression Depth
47.
Turn Off Safety Checks
Script Optimization
48.
Introduction
49.
Optimization Passes
❱
49.1.
Dead Code Elimination
49.2.
Constants Propagation
49.3.
Compound Assignment Rewrite
49.4.
Eager Operator Evaluation
49.5.
Eager Function Evaluation
❱
49.5.1.
Side-Effect Considerations
49.5.2.
Volatility Considerations
50.
Subtle Semantic Changes
51.
Re-Optimize an AST
Advanced Topics
52.
Manage AST's
53.
Low-Level API to Register Functions
54.
Evaluation Context
55.
Call Function Within Caller's Scope
56.
Use Rhai as a DSL
❱
56.1.
Remap Tokens During Parsing
56.2.
Disable Keywords and/or Operators
56.3.
Disable Looping
56.4.
Custom Operators
❱
56.4.1.
Operator Precedence
56.5.
Extend with Custom Syntax
57.
Debugging Interface
❱
57.1.
Debugger
❱
57.1.1.
State
57.1.2.
Call Stack
57.1.3.
Break-Points
57.2.
Debugging Server
Usage Patterns
58.
Object-Oriented Programming (OOP)
59.
Working with Rust Enums
60.
Simulate Macros to Simplify Scripts
61.
Dynamic Constants Provider
62.
Global Constants
63.
Mutable Global State
64.
Loadable Configuration
65.
Multi-Layered Functions
66.
Hot Reloading
67.
One Engine Instance Per Call
68.
Multi-Threaded Synchronization
69.
Control Layer Over Rust Backend
70.
Singleton Command Object
71.
Blocking/Async Function Calls
72.
Scriptable Event Handler with State
❱
72.1.
Main Style
72.2.
JS Style
72.3.
Map Style
73.
Domain-Specific Tools
74.
Multiple Instantiation
Language Reference
75.
Introduction
76.
Comments
77.
Value Types
❱
77.1.
Dynamic Values
❱
77.1.1.
type_of()
77.1.2.
Value Tag
77.2.
Numbers
❱
77.2.1.
Operators
77.2.2.
Standard Functions
77.2.3.
Value Conversions
77.2.4.
Ranges
77.2.5.
Bit-Fields
77.3.
Strings and Characters
❱
77.3.1.
Standard Functions and Operators
77.4.
Arrays
❱
77.4.1.
BLOB's (Byte Arrays)
77.5.
Object Maps
❱
77.5.1.
Special Support for OOP
77.6.
Timestamps
78.
Keywords
79.
Statements
80.
Variables
81.
Constants
82.
Assignments
83.
Operators
84.
Indexing
85.
Properties
86.
Methods
87.
If Statement
88.
Switch Statement
89.
While Loop
90.
Do Loop
91.
Infinite Loop
92.
For Loop
93.
Return Value
94.
Throw Exception on Error
❱
94.1.
Catch Exceptions
95.
Functions
❱
95.1.
Method Calls
95.2.
Overloading
95.3.
Function Pointers
❱
95.3.1.
Currying
95.3.2.
Anonymous Functions
95.3.3.
Closures
95.4.
Metadata
96.
Print and Debug
97.
Modules
❱
97.1.
Export Variables, Functions and Sub-Modules from Script
97.2.
Import Modules
98.
Eval Function
Appendix
99.
External Tools
❱
99.1.
Online Playground
99.2.
rhai-doc
100.
Keywords
101.
Operators and Symbols
102.
Literals
Light (default)
Rust
Coal
Navy
Ayu
Rhai - Rust 语言的嵌入式脚本
Rhai Language Reference
This is a stand-alone reference for the Rhai scripting language.