quad_compat_rhai/lib.rs
1//! # Rhai - embedded scripting for Rust
2//!
3//! 
4//!
5//! Rhai is a tiny, simple and fast embedded scripting language for Rust
6//! that gives you a safe and easy way to add scripting to your applications.
7//!
8//! It provides a familiar syntax based on JavaScript+Rust and a simple Rust interface.
9//!
10//! # A Quick Example
11//!
12//! ## Contents of `my_script.rhai`
13//!
14//! ```ignore
15//! /// Brute force factorial function
16//! fn factorial(x) {
17//! if x == 1 { return 1; }
18//! x * factorial(x - 1)
19//! }
20//!
21//! // Calling an external function 'compute'
22//! compute(factorial(10))
23//! ```
24//!
25//! ## The Rust part
26//!
27//! ```no_run
28//! use quad_compat_rhai::{Engine, EvalAltResult};
29//!
30//! fn main() -> Result<(), Box<EvalAltResult>>
31//! {
32//! // Define external function
33//! fn compute_something(x: i64) -> bool {
34//! (x % 40) == 0
35//! }
36//!
37//! // Create scripting engine
38//! let mut engine = Engine::new();
39//!
40//! // Register external function as 'compute'
41//! engine.register_fn("compute", compute_something);
42//!
43//! # #[cfg(not(feature = "no_std"))]
44//! # #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
45//! assert_eq!(
46//! // Evaluate the script, expects a 'bool' return
47//! engine.eval_file::<bool>("my_script.rhai".into())?,
48//! true
49//! );
50//!
51//! Ok(())
52//! }
53//! ```
54//!
55//! # Documentation
56//!
57//! See [The Rhai Book](https://rhai.rs/book) for details on the Rhai scripting engine and language.
58
59#![cfg_attr(feature = "no_std", no_std)]
60
61#[cfg(feature = "no_std")]
62extern crate alloc;
63
64#[cfg(feature = "no_std")]
65extern crate no_std_compat as std;
66
67#[cfg(feature = "no_std")]
68use std::prelude::v1::*;
69
70// Internal modules
71
72mod api;
73mod ast;
74mod custom_syntax;
75mod engine;
76mod func;
77mod module;
78mod optimizer;
79pub mod packages;
80mod parser;
81mod tests;
82mod tokenizer;
83mod types;
84mod r#unsafe;
85
86type RhaiResult = Result<Dynamic, Box<EvalAltResult>>;
87
88/// The system integer type. It is defined as [`i64`].
89///
90/// If the `only_i32` feature is enabled, this will be [`i32`] instead.
91#[cfg(not(feature = "only_i32"))]
92pub type INT = i64;
93
94/// The system integer type.
95/// It is defined as [`i32`] since the `only_i32` feature is used.
96///
97/// If the `only_i32` feature is not used, this will be `i64` instead.
98#[cfg(feature = "only_i32")]
99pub type INT = i32;
100
101/// The system floating-point type. It is defined as [`f64`].
102/// Not available under `no_float`.
103///
104/// If the `f32_float` feature is enabled, this will be [`f32`] instead.
105#[cfg(not(feature = "no_float"))]
106#[cfg(not(feature = "f32_float"))]
107pub type FLOAT = f64;
108
109/// The system floating-point type.
110/// It is defined as [`f32`] since the `f32_float` feature is used.
111/// Not available under `no_float`.
112///
113/// If the `f32_float` feature is not used, this will be `f64` instead.
114#[cfg(not(feature = "no_float"))]
115#[cfg(feature = "f32_float")]
116pub type FLOAT = f32;
117
118pub type ExclusiveRange = std::ops::Range<INT>;
119pub type InclusiveRange = std::ops::RangeInclusive<INT>;
120
121pub use ast::{FnAccess, AST};
122pub use custom_syntax::Expression;
123pub use engine::{
124 Engine, EvalContext, OP_CONTAINS, OP_EQUALS, OP_EXCLUSIVE_RANGE, OP_INCLUSIVE_RANGE,
125};
126pub use func::{NativeCallContext, RegisterNativeFunction};
127pub use module::{FnNamespace, Module};
128pub use tokenizer::Position;
129pub use types::{
130 Dynamic, EvalAltResult, FnPtr, ImmutableString, LexError, ParseError, ParseErrorType, Scope,
131};
132
133/// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most
134/// identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline.
135#[cfg(not(feature = "internals"))]
136pub(crate) type Identifier = SmartString;
137
138/// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most
139/// identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline.
140#[cfg(feature = "internals")]
141pub type Identifier = SmartString;
142
143/// Alias to [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc] depending on the `sync` feature flag.
144pub use func::Shared;
145
146/// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag.
147pub use func::Locked;
148
149pub(crate) use func::{
150 calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash,
151 combine_hashes,
152};
153
154pub use rhai_codegen::*;
155
156pub use func::{plugin, FuncArgs};
157
158#[cfg(not(feature = "no_function"))]
159pub use func::Func;
160
161#[cfg(not(feature = "no_function"))]
162pub use ast::ScriptFnMetadata;
163
164/// Variable-sized array of [`Dynamic`] values.
165/// Not available under `no_index`.
166#[cfg(not(feature = "no_index"))]
167pub type Array = Vec<Dynamic>;
168
169/// Variable-sized array of [`u8`] values (byte array).
170/// Not available under `no_index`.
171#[cfg(not(feature = "no_index"))]
172pub type Blob = Vec<u8>;
173
174/// Hash map of [`Dynamic`] values with [`SmartString`](https://crates.io/crates/smartstring) keys.
175/// Not available under `no_object`.
176#[cfg(not(feature = "no_object"))]
177pub type Map = std::collections::BTreeMap<Identifier, Dynamic>;
178
179#[cfg(not(feature = "no_module"))]
180pub use module::ModuleResolver;
181
182/// Module containing all built-in _module resolvers_ available to Rhai.
183#[cfg(not(feature = "no_module"))]
184pub use module::resolvers as module_resolvers;
185
186#[cfg(feature = "serde")]
187pub mod serde;
188
189#[cfg(not(feature = "no_optimize"))]
190pub use optimizer::OptimizationLevel;
191
192// Expose internal data structures.
193
194#[cfg(feature = "internals")]
195pub use types::dynamic::{AccessMode, DynamicReadLock, DynamicWriteLock, Variant};
196
197#[cfg(feature = "internals")]
198pub use tokenizer::{get_next_token, parse_string_literal};
199
200#[cfg(feature = "internals")]
201pub use tokenizer::{
202 InputStream, MultiInputsStream, Token, TokenIterator, TokenizeState, TokenizerControl,
203 TokenizerControlBlock,
204};
205
206#[cfg(feature = "internals")]
207pub use parser::{IdentifierBuilder, ParseState};
208
209#[cfg(feature = "internals")]
210pub use ast::{
211 ASTNode, BinaryExpr, CustomExpr, Expr, FnCallExpr, FnCallHashes, Ident, OpAssignment,
212 OptionFlags, ScriptFnDef, Stmt, StmtBlock, AST_OPTION_FLAGS::*,
213};
214
215#[cfg(feature = "internals")]
216#[cfg(not(feature = "no_float"))]
217pub use ast::FloatWrapper;
218
219#[cfg(feature = "internals")]
220pub use engine::{EvalState, FnResolutionCache, FnResolutionCacheEntry, Imports};
221
222#[cfg(feature = "internals")]
223pub use module::NamespaceRef;
224
225/// Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec), which is a
226/// specialized [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
227///
228/// # History
229///
230/// And Saint Attila raised the `SmallVec` up on high, saying, "O Lord, bless this Thy `SmallVec`
231/// that, with it, Thou mayest blow Thine allocation costs to tiny bits in Thy mercy."
232///
233/// And the Lord did grin, and the people did feast upon the lambs and sloths and carp and anchovies
234/// and orangutans and breakfast cereals and fruit bats and large chu...
235///
236/// And the Lord spake, saying, "First shalt thou depend on the [`smallvec`](https://crates.io/crates/smallvec) crate.
237/// Then, shalt thou keep three inline. No more. No less. Three shalt be the number thou shalt keep inline,
238/// and the number to keep inline shalt be three. Four shalt thou not keep inline, nor either keep inline
239/// thou two, excepting that thou then proceed to three. Five is right out. Once the number three,
240/// being the third number, be reached, then, lobbest thou thy `SmallVec` towards thy heap, who,
241/// being slow and cache-naughty in My sight, shall snuff it."
242///
243/// # Why Three
244///
245/// `StaticVec` is used frequently to keep small lists of items in inline (non-heap) storage in
246/// order to improve cache friendliness and reduce indirections.
247///
248/// The number 3, other than being the holy number, is carefully chosen for a balance between
249/// storage space and reduce allocations. That is because most function calls (and most functions,
250/// for that matter) contain fewer than 4 arguments, the exception being closures that capture a
251/// large number of external variables.
252///
253/// In addition, most script blocks either contain many statements, or just one or two lines;
254/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
255/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
256#[cfg(not(feature = "internals"))]
257type StaticVec<T> = smallvec::SmallVec<[T; 3]>;
258
259/// _(internals)_ Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec),
260/// which is a [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
261/// Exported under the `internals` feature only.
262///
263/// # History
264///
265/// And Saint Attila raised the `SmallVec` up on high, saying, "O Lord, bless this Thy `SmallVec`
266/// that, with it, Thou mayest blow Thine allocation costs to tiny bits in Thy mercy."
267///
268/// And the Lord did grin, and the people did feast upon the lambs and sloths and carp and anchovies
269/// and orangutans and breakfast cereals and fruit bats and large chu...
270///
271/// And the Lord spake, saying, "First shalt thou depend on the [`smallvec`](https://crates.io/crates/smallvec) crate.
272/// Then, shalt thou keep three inline. No more. No less. Three shalt be the number thou shalt keep inline,
273/// and the number to keep inline shalt be three. Four shalt thou not keep inline, nor either keep inline
274/// thou two, excepting that thou then proceed to three. Five is right out. Once the number three,
275/// being the third number, be reached, then, lobbest thou thy `SmallVec` towards thy heap, who,
276/// being slow and cache-naughty in My sight, shall snuff it."
277///
278/// # Why Three
279///
280/// `StaticVec` is used frequently to keep small lists of items in inline (non-heap) storage in
281/// order to improve cache friendliness and reduce indirections.
282///
283/// The number 3, other than being the holy number, is carefully chosen for a balance between
284/// storage space and reduce allocations. That is because most function calls (and most functions,
285/// for that matter) contain fewer than 4 arguments, the exception being closures that capture a
286/// large number of external variables.
287///
288/// In addition, most script blocks either contain many statements, or just one or two lines;
289/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
290/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
291#[cfg(feature = "internals")]
292pub type StaticVec<T> = smallvec::SmallVec<[T; 3]>;
293
294pub(crate) type SmartString = smartstring::SmartString<smartstring::LazyCompact>;
295
296// Compiler guards against mutually-exclusive feature flags
297
298#[cfg(feature = "no_float")]
299#[cfg(feature = "f32_float")]
300compile_error!("`f32_float` cannot be used with `no_float`");
301
302#[cfg(feature = "only_i32")]
303#[cfg(feature = "only_i64")]
304compile_error!("`only_i32` and `only_i64` cannot be used together");
305
306#[cfg(feature = "no_std")]
307#[cfg(feature = "wasm-bindgen")]
308compile_error!("`wasm-bindgen` cannot be used with `no-std`");
309
310#[cfg(feature = "no_std")]
311#[cfg(feature = "stdweb")]
312compile_error!("`stdweb` cannot be used with `no-std`");
313
314#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
315#[cfg(feature = "no_std")]
316compile_error!("`no_std` cannot be used for WASM target");
317
318#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
319#[cfg(feature = "wasm-bindgen")]
320compile_error!("`wasm-bindgen` cannot be used for non-WASM target");
321
322#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
323#[cfg(feature = "stdweb")]
324compile_error!("`stdweb` cannot be used non-WASM target");
325
326#[cfg(feature = "wasm-bindgen")]
327#[cfg(feature = "stdweb")]
328compile_error!("`wasm-bindgen` and `stdweb` cannot be used together");