spo_rhai/
lib.rs

1//! # Rhai - embedded scripting for Rust
2//!
3//! ![Rhai logo](https://rhai.rs/book/images/logo/rhai-banner-transparent-colour.svg)
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//! ```rhai
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 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(target_family = "wasm"))]
45//! #
46//!     // Evaluate the script, expecting a 'bool' result
47//!     let result: bool = engine.eval_file("my_script.rhai".into())?;
48//!
49//!     assert_eq!(result, true);
50//!
51//!     Ok(())
52//! }
53//! ```
54//!
55//! # Features
56//!
57#![cfg_attr(feature = "document-features", doc = document_features::document_features!(
58    feature_label = "<span id=\"feature-{feature}\">**`{feature}`**</span>"
59))]
60//!
61//! # On-Line Documentation
62//!
63//! See [The Rhai Book](https://rhai.rs/book) for details on the Rhai scripting engine and language.
64
65// Map `no_std` feature.
66#![cfg_attr(feature = "no_std", no_std)]
67//
68// Clippy lints.
69//
70#![deny(missing_docs)]
71// #![warn(clippy::all)]
72// #![warn(clippy::pedantic)]
73// #![warn(clippy::nursery)]
74#![warn(clippy::cargo)]
75#![warn(clippy::undocumented_unsafe_blocks)]
76#![allow(clippy::missing_errors_doc)]
77#![allow(clippy::missing_panics_doc)]
78#![allow(clippy::used_underscore_binding)]
79#![allow(clippy::inline_always)]
80#![allow(clippy::module_name_repetitions)]
81#![allow(clippy::negative_feature_names)]
82#![allow(clippy::box_collection)]
83#![allow(clippy::upper_case_acronyms)]
84#![allow(clippy::match_same_arms)]
85#![allow(clippy::unnecessary_box_returns)]
86// The lints below are turned off to reduce signal/noise ratio
87#![allow(clippy::cognitive_complexity)] // Hey, this is a scripting engine with a compiler...
88#![allow(clippy::too_many_lines)] // Same...
89#![allow(clippy::too_many_arguments)] // Same...
90#![allow(clippy::absurd_extreme_comparisons)] // On `only_i32`, `MAX_USIZE_INT` < `INT::MAX` because `usize` == `u32` and `INT` == `i64`
91#![allow(clippy::wildcard_imports)] // Wildcard imports are used to import the plugins prelude
92#![allow(clippy::enum_glob_use)] // Sometimes useful to import all `Tokens` etc.
93#![allow(clippy::no_effect_underscore_binding)] // Underscored variables may be used by code within feature guards
94#![allow(clippy::semicolon_if_nothing_returned)] // One-liner `match` cases are sometimes formatted as multi-line blocks
95
96#[cfg(feature = "no_std")]
97extern crate alloc;
98
99#[cfg(feature = "no_std")]
100extern crate no_std_compat as std;
101
102#[cfg(feature = "no_std")]
103use std::prelude::v1::*;
104
105// Internal modules
106#[macro_use]
107mod reify;
108#[macro_use]
109mod defer;
110
111mod api;
112mod ast;
113pub mod config;
114mod engine;
115mod eval;
116mod func;
117mod module;
118mod optimizer;
119pub mod packages;
120mod parser;
121#[cfg(feature = "serde")]
122pub mod serde;
123mod tests;
124mod tokenizer;
125mod types;
126
127/// Error encountered when parsing a script.
128type PERR = ParseErrorType;
129/// Evaluation result.
130type ERR = EvalAltResult;
131/// General evaluation error for Rhai scripts.
132type RhaiError = Box<ERR>;
133/// Generic [`Result`] type for Rhai functions.
134type RhaiResultOf<T> = Result<T, RhaiError>;
135/// General [`Result`] type for Rhai functions returning [`Dynamic`] values.
136type RhaiResult = RhaiResultOf<Dynamic>;
137
138/// The system integer type. It is defined as [`i64`].
139///
140/// If the `only_i32` feature is enabled, this will be [`i32`] instead.
141#[cfg(not(feature = "only_i32"))]
142pub type INT = i64;
143
144/// The system integer type.
145/// It is defined as [`i32`] since the `only_i32` feature is used.
146///
147/// If the `only_i32` feature is not used, this will be `i64` instead.
148#[cfg(feature = "only_i32")]
149pub type INT = i32;
150
151/// The unsigned system base integer type. It is defined as [`u64`].
152///
153/// If the `only_i32` feature is enabled, this will be [`u32`] instead.
154#[cfg(not(feature = "only_i32"))]
155#[allow(non_camel_case_types)]
156type UNSIGNED_INT = u64;
157
158/// The unsigned system integer base type.
159/// It is defined as [`u32`] since the `only_i32` feature is used.
160///
161/// If the `only_i32` feature is not used, this will be `u64` instead.
162#[cfg(feature = "only_i32")]
163#[allow(non_camel_case_types)]
164type UNSIGNED_INT = u32;
165
166/// The maximum integer that can fit into a [`usize`].
167#[cfg(not(target_pointer_width = "32"))]
168const MAX_USIZE_INT: INT = INT::MAX;
169
170/// The maximum integer that can fit into a [`usize`].
171#[cfg(not(feature = "only_i32"))]
172#[cfg(target_pointer_width = "32")]
173const MAX_USIZE_INT: INT = usize::MAX as INT;
174
175/// The maximum integer that can fit into a [`usize`].
176#[cfg(feature = "only_i32")]
177#[cfg(target_pointer_width = "32")]
178const MAX_USIZE_INT: INT = INT::MAX;
179
180/// Number of bits in [`INT`].
181///
182/// It is 64 unless the `only_i32` feature is enabled when it will be 32.
183const INT_BITS: usize = std::mem::size_of::<INT>() * 8;
184
185/// Number of bytes that make up an [`INT`].
186///
187/// It is 8 unless the `only_i32` feature is enabled when it will be 4.
188#[cfg(not(feature = "no_index"))]
189const INT_BYTES: usize = std::mem::size_of::<INT>();
190
191/// The system floating-point type. It is defined as [`f64`].
192///
193/// Not available under `no_float`.
194///
195/// If the `f32_float` feature is enabled, this will be [`f32`] instead.
196#[cfg(not(feature = "no_float"))]
197#[cfg(not(feature = "f32_float"))]
198pub type FLOAT = f64;
199
200/// The system floating-point type.
201/// It is defined as [`f32`] since the `f32_float` feature is used.
202///
203/// Not available under `no_float`.
204///
205/// If the `f32_float` feature is not used, this will be `f64` instead.
206#[cfg(not(feature = "no_float"))]
207#[cfg(feature = "f32_float")]
208pub type FLOAT = f32;
209
210/// Number of bytes that make up a [`FLOAT`].
211///
212/// It is 8 unless the `f32_float` feature is enabled when it will be 4.
213#[cfg(not(feature = "no_float"))]
214#[cfg(not(feature = "no_index"))]
215const FLOAT_BYTES: usize = std::mem::size_of::<FLOAT>();
216
217/// An exclusive integer range.
218type ExclusiveRange = std::ops::Range<INT>;
219
220/// An inclusive integer range.
221type InclusiveRange = std::ops::RangeInclusive<INT>;
222
223#[cfg(feature = "std")]
224use once_cell::sync::OnceCell;
225
226#[cfg(not(feature = "std"))]
227use once_cell::race::OnceBox as OnceCell;
228
229pub use api::build_type::{CustomType, TypeBuilder};
230#[cfg(not(feature = "no_custom_syntax"))]
231pub use api::custom_syntax::Expression;
232#[cfg(not(feature = "no_std"))]
233#[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
234pub use api::files::{eval_file, run_file};
235pub use api::{eval::eval, run::run};
236pub use ast::{FnAccess, AST};
237use defer::Deferred;
238pub use engine::{Engine, OP_CONTAINS, OP_EQUALS};
239pub use eval::EvalContext;
240#[cfg(not(feature = "no_function"))]
241#[cfg(not(feature = "no_object"))]
242use func::calc_typed_method_hash;
243use func::{calc_fn_hash, calc_fn_hash_full, calc_var_hash};
244pub use func::{plugin, FuncArgs, NativeCallContext, RhaiNativeFunc};
245pub use module::{FnNamespace, FuncRegistration, Module};
246pub use packages::string_basic::{FUNC_TO_DEBUG, FUNC_TO_STRING};
247pub use rhai_codegen::*;
248#[cfg(not(feature = "no_time"))]
249pub use types::Instant;
250pub use types::{
251    Dynamic, EvalAltResult, FnPtr, ImmutableString, LexError, ParseError, ParseErrorType, Position,
252    Scope, VarDefInfo,
253};
254
255/// _(debugging)_ Module containing types for debugging.
256/// Exported under the `debugging` feature only.
257#[cfg(feature = "debugging")]
258pub mod debugger {
259    #[cfg(not(feature = "no_function"))]
260    pub use super::eval::CallStackFrame;
261    pub use super::eval::{BreakPoint, Debugger, DebuggerCommand, DebuggerEvent};
262}
263
264/// An identifier in Rhai. [`SmartString`](https://crates.io/crates/smartstring) is used because most
265/// identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline.
266#[cfg(not(feature = "internals"))]
267type Identifier = SmartString;
268
269/// An identifier in Rhai.
270///
271/// Identifiers are assumed to be all-ASCII and short with few exceptions.
272///
273/// [`SmartString`](https://crates.io/crates/smartstring) is used as the underlying storage type
274/// because most identifiers can be stored inline.
275#[cfg(feature = "internals")]
276pub type Identifier = SmartString;
277
278/// Alias to [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc] depending on the `sync` feature flag.
279pub use func::Shared;
280
281/// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag.
282pub use func::Locked;
283
284/// A shared [`Module`].
285type SharedModule = Shared<Module>;
286
287#[cfg(not(feature = "no_function"))]
288pub use func::Func;
289
290#[cfg(not(feature = "no_function"))]
291pub use ast::ScriptFnMetadata;
292
293#[cfg(not(feature = "no_function"))]
294pub use api::call_fn::CallFnOptions;
295
296/// Variable-sized array of [`Dynamic`] values.
297///
298/// Not available under `no_index`.
299#[cfg(not(feature = "no_index"))]
300pub type Array = Vec<Dynamic>;
301
302/// Variable-sized array of [`u8`] values (byte array).
303///
304/// Not available under `no_index`.
305#[cfg(not(feature = "no_index"))]
306pub type Blob = Vec<u8>;
307
308/// A dictionary of [`Dynamic`] values with string keys.
309///
310/// Not available under `no_object`.
311///
312/// [`SmartString`](https://crates.io/crates/smartstring) is used as the key type because most
313/// property names are ASCII and short, fewer than 23 characters, so they can be stored inline.
314#[cfg(not(feature = "no_object"))]
315#[cfg(feature = "indexmap")]
316pub type Map = indexmap::IndexMap<Identifier, Dynamic>;
317
318/// A dictionary of [`Dynamic`] values with string keys.
319///
320/// Not available under `no_object`.
321///
322/// [`SmartString`](https://crates.io/crates/smartstring) is used as the key type because most
323/// property names are ASCII and short, fewer than 23 characters, so they can be stored inline.
324#[cfg(not(feature = "no_object"))]
325#[cfg(not(feature = "indexmap"))]
326pub type Map = std::collections::BTreeMap<Identifier, Dynamic>;
327
328#[cfg(not(feature = "no_object"))]
329pub use api::json::format_map_as_json;
330
331#[cfg(not(feature = "no_module"))]
332pub use module::ModuleResolver;
333
334/// Module containing all built-in _module resolvers_ available to Rhai.
335#[cfg(not(feature = "no_module"))]
336pub use module::resolvers as module_resolvers;
337
338#[cfg(not(feature = "no_optimize"))]
339pub use optimizer::OptimizationLevel;
340
341/// Placeholder for the optimization level.
342#[cfg(feature = "no_optimize")]
343type OptimizationLevel = ();
344
345// Expose internal data structures.
346
347#[cfg(feature = "internals")]
348pub use types::dynamic::{AccessMode, DynamicReadLock, DynamicWriteLock, Variant};
349
350#[cfg(feature = "internals")]
351#[cfg(not(feature = "no_float"))]
352pub use types::FloatWrapper;
353
354#[cfg(feature = "internals")]
355pub use types::{BloomFilterU64, CustomTypeInfo, Span, StringsInterner};
356
357#[cfg(feature = "internals")]
358pub use tokenizer::{
359    get_next_token, is_valid_function_name, is_valid_identifier, parse_string_literal, InputStream,
360    MultiInputsStream, Token, TokenIterator, TokenizeState, TokenizerControl,
361    TokenizerControlBlock,
362};
363
364#[cfg(feature = "internals")]
365pub use parser::ParseState;
366
367#[cfg(feature = "internals")]
368pub use api::default_limits;
369
370#[cfg(feature = "internals")]
371pub use ast::{
372    ASTFlags, ASTNode, BinaryExpr, EncapsulatedEnviron, Expr, FlowControl, FnCallExpr,
373    FnCallHashes, Ident, OpAssignment, RangeCase, ScriptFuncDef, Stmt, StmtBlock,
374    SwitchCasesCollection,
375};
376
377#[cfg(feature = "internals")]
378#[cfg(not(feature = "no_custom_syntax"))]
379pub use ast::CustomExpr;
380
381#[cfg(feature = "internals")]
382#[cfg(not(feature = "no_module"))]
383pub use ast::Namespace;
384
385#[cfg(feature = "internals")]
386pub use eval::{Caches, FnResolutionCache, FnResolutionCacheEntry, GlobalRuntimeState};
387
388#[cfg(feature = "internals")]
389#[allow(deprecated)]
390pub use func::{locked_read, locked_write, NativeCallContextStore, RhaiFunc};
391
392#[cfg(feature = "internals")]
393#[cfg(feature = "metadata")]
394pub use api::definitions::Definitions;
395
396/// Number of items to keep inline for [`StaticVec`].
397const STATIC_VEC_INLINE_SIZE: usize = 3;
398
399/// Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec).
400#[cfg(not(feature = "internals"))]
401type StaticVec<T> = smallvec::SmallVec<[T; STATIC_VEC_INLINE_SIZE]>;
402
403/// _(internals)_ Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec),
404/// which is a [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
405/// Exported under the `internals` feature only.
406///
407/// # History
408///
409/// And Saint Attila raised the `SmallVec` up on high, saying, "O Lord, bless this Thy `SmallVec`
410/// that, with it, Thou mayest blow Thine allocation costs to tiny bits in Thy mercy."
411///
412/// And the Lord did grin, and the people did feast upon the lambs and sloths and carp and anchovies
413/// and orangutans and breakfast cereals and fruit bats and large chu...
414///
415/// And the Lord spake, saying, "First shalt thou depend on the [`smallvec`](https://crates.io/crates/smallvec) crate.
416/// Then, shalt thou keep three inline. No more. No less. Three shalt be the number thou shalt keep inline,
417/// and the number to keep inline shalt be three. Four shalt thou not keep inline, nor either keep inline
418/// thou two, excepting that thou then proceed to three. Five is right out. Once the number three,
419/// being the third number, be reached, then, lobbest thou thy `SmallVec` towards thy heap, who,
420/// being slow and cache-naughty in My sight, shall snuff it."
421///
422/// # Why Three
423///
424/// `StaticVec` is used frequently to keep small lists of items in inline (non-heap) storage in
425/// order to improve cache friendliness and reduce indirections.
426///
427/// The number 3, other than being the holy number, is carefully chosen for a balance between
428/// storage space and reduce allocations. That is because most function calls (and most functions,
429/// for that matter) contain fewer than 4 arguments, the exception being closures that capture a
430/// large number of external variables.
431///
432/// In addition, most script blocks either contain many statements, or just one or two lines;
433/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
434/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
435#[cfg(feature = "internals")]
436pub type StaticVec<T> = smallvec::SmallVec<[T; STATIC_VEC_INLINE_SIZE]>;
437
438/// A smaller [`Vec`] alternative.
439#[cfg(not(feature = "internals"))]
440type ThinVec<T> = thin_vec::ThinVec<T>;
441
442/// _(internals)_ A smaller [`Vec`] alternative. Exported under the `internals` feature only.
443///
444/// The standard [`Vec`] type uses three machine words (i.e. 24 bytes on 64-bit).
445///
446/// [`ThinVec`](https://crates.io/crates/thin-vec) only uses one machine word, storing other
447/// information inline together with the data.
448///
449/// This is primarily used in places where a few bytes affect the size of the type
450/// -- e.g. in `enum`'s.
451#[cfg(feature = "internals")]
452pub type ThinVec<T> = thin_vec::ThinVec<T>;
453
454/// Number of items to keep inline for [`FnArgsVec`].
455#[cfg(not(feature = "no_closure"))]
456const FN_ARGS_VEC_INLINE_SIZE: usize = 5;
457
458/// Inline arguments storage for function calls.
459#[cfg(not(feature = "no_closure"))]
460#[cfg(not(feature = "internals"))]
461type FnArgsVec<T> = smallvec::SmallVec<[T; FN_ARGS_VEC_INLINE_SIZE]>;
462
463/// _(internals)_ Inline arguments storage for function calls.
464///
465/// Not available under `no_closure`.
466///
467/// # Notes
468///
469/// Since most usage of this is during a function call to gather up arguments, this is mostly
470/// allocated on the stack, so we can tolerate a larger number of values stored inline.
471///
472/// Most functions have few parameters, but closures with a lot of captured variables can
473/// potentially have many.  Having a larger inline storage for arguments reduces allocations in
474/// scripts with heavy closure usage.
475///
476/// Under `no_closure`, this type aliases to [`StaticVec`][crate::StaticVec] instead.
477#[cfg(not(feature = "no_closure"))]
478#[cfg(feature = "internals")]
479pub type FnArgsVec<T> = smallvec::SmallVec<[T; FN_ARGS_VEC_INLINE_SIZE]>;
480
481/// Inline arguments storage for function calls.
482/// This type aliases to [`StaticVec`][crate::StaticVec].
483#[cfg(feature = "no_closure")]
484type FnArgsVec<T> = crate::StaticVec<T>;
485
486type SmartString = smartstring::SmartString<smartstring::LazyCompact>;
487
488// Compiler guards against mutually-exclusive feature flags
489
490#[cfg(feature = "no_float")]
491#[cfg(feature = "f32_float")]
492compile_error!("`f32_float` cannot be used with `no_float`");
493
494#[cfg(feature = "only_i32")]
495#[cfg(feature = "only_i64")]
496compile_error!("`only_i32` and `only_i64` cannot be used together");
497
498#[cfg(feature = "no_std")]
499#[cfg(feature = "wasm-bindgen")]
500compile_error!("`wasm-bindgen` cannot be used with `no-std`");
501
502#[cfg(feature = "no_std")]
503#[cfg(feature = "stdweb")]
504compile_error!("`stdweb` cannot be used with `no-std`");
505
506#[cfg(target_family = "wasm")]
507#[cfg(feature = "no_std")]
508compile_error!("`no_std` cannot be used for WASM target");
509
510#[cfg(not(target_family = "wasm"))]
511#[cfg(feature = "wasm-bindgen")]
512compile_error!("`wasm-bindgen` cannot be used for non-WASM target");
513
514#[cfg(not(target_family = "wasm"))]
515#[cfg(feature = "stdweb")]
516compile_error!("`stdweb` cannot be used non-WASM target");
517
518#[cfg(feature = "wasm-bindgen")]
519#[cfg(feature = "stdweb")]
520compile_error!("`wasm-bindgen` and `stdweb` cannot be used together");