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// TODO: Further audit no_std compatibility
97// When building with no_std + sync features, explicit imports from alloc
98// are needed despite using no_std_compat. This fixed compilation errors
99// in `native.rs` around missing trait implementations for some users.
100//#[cfg(feature = "no_std")]
101extern crate alloc;
102
103#[cfg(feature = "no_std")]
104extern crate no_std_compat as std;
105
106#[cfg(feature = "no_std")]
107use std::prelude::v1::*;
108
109// Internal modules
110#[macro_use]
111mod reify;
112#[macro_use]
113mod defer;
114
115mod api;
116mod ast;
117pub mod config;
118mod engine;
119mod eval;
120mod func;
121mod module;
122mod optimizer;
123pub mod packages;
124mod parser;
125#[cfg(feature = "serde")]
126pub mod serde;
127mod tests;
128mod tokenizer;
129mod types;
130
131/// Error encountered when parsing a script.
132type PERR = ParseErrorType;
133/// Evaluation result.
134type ERR = EvalAltResult;
135/// General evaluation error for Rhai scripts.
136type RhaiError = Box<ERR>;
137/// Generic [`Result`] type for Rhai functions.
138type RhaiResultOf<T> = Result<T, RhaiError>;
139/// General [`Result`] type for Rhai functions returning [`Dynamic`] values.
140type RhaiResult = RhaiResultOf<Dynamic>;
141
142/// The system integer type. It is defined as [`i64`].
143///
144/// If the `only_i32` feature is enabled, this will be [`i32`] instead.
145#[cfg(not(feature = "only_i32"))]
146pub type INT = i64;
147
148/// The system integer type.
149/// It is defined as [`i32`] since the `only_i32` feature is used.
150///
151/// If the `only_i32` feature is not used, this will be `i64` instead.
152#[cfg(feature = "only_i32")]
153pub type INT = i32;
154
155/// The unsigned system base integer type. It is defined as [`u64`].
156///
157/// If the `only_i32` feature is enabled, this will be [`u32`] instead.
158#[cfg(not(feature = "only_i32"))]
159#[allow(non_camel_case_types)]
160type UNSIGNED_INT = u64;
161
162/// The unsigned system integer base type.
163/// It is defined as [`u32`] since the `only_i32` feature is used.
164///
165/// If the `only_i32` feature is not used, this will be `u64` instead.
166#[cfg(feature = "only_i32")]
167#[allow(non_camel_case_types)]
168type UNSIGNED_INT = u32;
169
170/// The maximum integer that can fit into a [`usize`].
171#[cfg(not(target_pointer_width = "32"))]
172const MAX_USIZE_INT: INT = INT::MAX;
173
174/// The maximum integer that can fit into a [`usize`].
175#[cfg(not(feature = "only_i32"))]
176#[cfg(target_pointer_width = "32")]
177const MAX_USIZE_INT: INT = usize::MAX as INT;
178
179/// The maximum integer that can fit into a [`usize`].
180#[cfg(feature = "only_i32")]
181#[cfg(target_pointer_width = "32")]
182const MAX_USIZE_INT: INT = INT::MAX;
183
184/// Number of bits in [`INT`].
185///
186/// It is 64 unless the `only_i32` feature is enabled when it will be 32.
187const INT_BITS: usize = std::mem::size_of::<INT>() * 8;
188
189/// Number of bytes that make up an [`INT`].
190///
191/// It is 8 unless the `only_i32` feature is enabled when it will be 4.
192#[cfg(not(feature = "no_index"))]
193const INT_BYTES: usize = std::mem::size_of::<INT>();
194
195/// The system floating-point type. It is defined as [`f64`].
196///
197/// Not available under `no_float`.
198///
199/// If the `f32_float` feature is enabled, this will be [`f32`] instead.
200#[cfg(not(feature = "no_float"))]
201#[cfg(not(feature = "f32_float"))]
202pub type FLOAT = f64;
203
204/// The system floating-point type.
205/// It is defined as [`f32`] since the `f32_float` feature is used.
206///
207/// Not available under `no_float`.
208///
209/// If the `f32_float` feature is not used, this will be `f64` instead.
210#[cfg(not(feature = "no_float"))]
211#[cfg(feature = "f32_float")]
212pub type FLOAT = f32;
213
214/// Number of bytes that make up a [`FLOAT`].
215///
216/// It is 8 unless the `f32_float` feature is enabled when it will be 4.
217#[cfg(not(feature = "no_float"))]
218#[cfg(not(feature = "no_index"))]
219const FLOAT_BYTES: usize = std::mem::size_of::<FLOAT>();
220
221/// An exclusive integer range.
222type ExclusiveRange = std::ops::Range<INT>;
223
224/// An inclusive integer range.
225type InclusiveRange = std::ops::RangeInclusive<INT>;
226
227#[cfg(feature = "std")]
228use once_cell::sync::OnceCell;
229
230#[cfg(not(feature = "std"))]
231use once_cell::race::OnceBox as OnceCell;
232
233pub use api::build_type::{CustomType, TypeBuilder};
234#[cfg(not(feature = "no_custom_syntax"))]
235pub use api::custom_syntax::Expression;
236#[cfg(not(feature = "no_std"))]
237#[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
238pub use api::files::{eval_file, run_file};
239pub use api::{eval::eval, run::run};
240pub use ast::{FnAccess, AST};
241use defer::Deferred;
242pub use engine::{Engine, OP_CONTAINS, OP_EQUALS};
243pub use eval::EvalContext;
244#[cfg(not(feature = "no_function"))]
245#[cfg(not(feature = "no_object"))]
246use func::calc_typed_method_hash;
247use func::{calc_fn_hash, calc_fn_hash_full, calc_var_hash};
248pub use func::{plugin, FuncArgs, NativeCallContext, RhaiNativeFunc};
249pub use module::{FnNamespace, FuncRegistration, Module};
250pub use packages::string_basic::{FUNC_TO_DEBUG, FUNC_TO_STRING};
251pub use rhai_codegen::*;
252#[cfg(not(feature = "no_time"))]
253pub use types::Instant;
254pub use types::{
255    Dynamic, EvalAltResult, FnPtr, ImmutableString, LexError, ParseError, ParseErrorType, Position,
256    Scope, VarDefInfo,
257};
258
259/// _(debugging)_ Module containing types for debugging.
260/// Exported under the `debugging` feature only.
261#[cfg(feature = "debugging")]
262pub mod debugger {
263    #[cfg(not(feature = "no_function"))]
264    pub use super::eval::CallStackFrame;
265    pub use super::eval::{BreakPoint, Debugger, DebuggerCommand, DebuggerEvent};
266}
267
268/// _(internals)_ An identifier in Rhai.
269/// Exported under the `internals` feature only.
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#[expose_under_internals]
276type 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"))]
315pub type Map = std::collections::BTreeMap<Identifier, Dynamic>;
316
317#[cfg(not(feature = "no_object"))]
318pub use api::json::format_map_as_json;
319
320#[cfg(not(feature = "no_module"))]
321pub use module::ModuleResolver;
322
323/// Module containing all built-in _module resolvers_ available to Rhai.
324#[cfg(not(feature = "no_module"))]
325pub use module::resolvers as module_resolvers;
326
327#[cfg(not(feature = "no_optimize"))]
328pub use optimizer::OptimizationLevel;
329
330// Expose internal data structures.
331
332#[cfg(feature = "internals")]
333pub use types::dynamic::{AccessMode, DynamicReadLock, DynamicWriteLock, Variant};
334
335#[cfg(feature = "internals")]
336pub use module::{FuncInfo, FuncMetadata};
337
338#[cfg(feature = "internals")]
339#[cfg(not(feature = "no_float"))]
340pub use types::FloatWrapper;
341
342#[cfg(feature = "internals")]
343pub use types::{BloomFilterU64, CustomTypeInfo, Span, StringsInterner};
344
345#[cfg(feature = "internals")]
346pub use tokenizer::{
347    get_next_token, is_valid_function_name, is_valid_identifier, parse_raw_string_literal,
348    parse_string_literal, InputStream, MultiInputsStream, Token, TokenIterator, TokenizeState,
349    TokenizerControl, TokenizerControlBlock,
350};
351
352#[cfg(feature = "internals")]
353pub use parser::ParseState;
354
355#[cfg(feature = "internals")]
356pub use api::default_limits;
357
358#[cfg(feature = "internals")]
359pub use ast::{
360    ASTFlags, ASTNode, BinaryExpr, EncapsulatedEnviron, Expr, FlowControl, FnCallExpr,
361    FnCallHashes, Ident, OpAssignment, RangeCase, ScriptFuncDef, Stmt, StmtBlock,
362    SwitchCasesCollection,
363};
364
365#[cfg(feature = "internals")]
366#[cfg(not(feature = "no_custom_syntax"))]
367pub use ast::CustomExpr;
368
369#[cfg(feature = "internals")]
370#[cfg(not(feature = "no_module"))]
371pub use ast::Namespace;
372
373#[cfg(feature = "internals")]
374pub use eval::{Caches, FnResolutionCache, FnResolutionCacheEntry, GlobalRuntimeState, Target};
375
376#[cfg(feature = "internals")]
377#[allow(deprecated)]
378pub use func::{locked_read, locked_write, NativeCallContextStore, RhaiFunc};
379
380#[cfg(feature = "internals")]
381#[cfg(feature = "metadata")]
382pub use api::definitions::Definitions;
383
384/// Number of items to keep inline for [`StaticVec`].
385const STATIC_VEC_INLINE_SIZE: usize = 3;
386
387/// _(internals)_ Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec),
388/// which is a [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored.
389/// Exported under the `internals` feature only.
390///
391/// # History
392///
393/// And Saint Attila raised the `SmallVec` up on high, saying, "O Lord, bless this Thy `SmallVec`
394/// that, with it, Thou mayest blow Thine allocation costs to tiny bits in Thy mercy."
395///
396/// And the Lord did grin, and the people did feast upon the lambs and sloths and carp and anchovies
397/// and orangutans and breakfast cereals and fruit bats and large chu...
398///
399/// And the Lord spake, saying, "First shalt thou depend on the [`smallvec`](https://crates.io/crates/smallvec) crate.
400/// Then, shalt thou keep three inline. No more. No less. Three shalt be the number thou shalt keep inline,
401/// and the number to keep inline shalt be three. Four shalt thou not keep inline, nor either keep inline
402/// thou two, excepting that thou then proceed to three. Five is right out. Once the number three,
403/// being the third number, be reached, then, lobbest thou thy `SmallVec` towards thy heap, who,
404/// being slow and cache-naughty in My sight, shall snuff it."
405///
406/// # Why Three
407///
408/// `StaticVec` is used frequently to keep small lists of items in inline (non-heap) storage in
409/// order to improve cache friendliness and reduce indirections.
410///
411/// The number 3, other than being the holy number, is carefully chosen for a balance between
412/// storage space and reduce allocations. That is because most function calls (and most functions,
413/// for that matter) contain fewer than 4 arguments, the exception being closures that capture a
414/// large number of external variables.
415///
416/// In addition, most script blocks either contain many statements, or just one or two lines;
417/// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels
418/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get).
419#[expose_under_internals]
420type StaticVec<T> = smallvec::SmallVec<[T; STATIC_VEC_INLINE_SIZE]>;
421
422/// _(internals)_ A smaller [`Vec`] alternative. Exported under the `internals` feature only.
423/// Exported under the `internals` feature only.
424///
425/// The standard [`Vec`] type uses three machine words (i.e. 24 bytes on 64-bit).
426///
427/// [`ThinVec`](https://crates.io/crates/thin-vec) only uses one machine word, storing other
428/// information inline together with the data.
429///
430/// This is primarily used in places where a few bytes affect the size of the type
431/// -- e.g. in `enum`'s.
432#[expose_under_internals]
433type ThinVec<T> = thin_vec::ThinVec<T>;
434
435/// Number of items to keep inline for [`FnArgsVec`].
436#[cfg(not(feature = "no_closure"))]
437const FN_ARGS_VEC_INLINE_SIZE: usize = 5;
438
439/// _(internals)_ Inline arguments storage for function calls.
440/// Exported under the `internals` feature only.
441///
442/// Not available under `no_closure`.
443///
444/// # Notes
445///
446/// Since most usage of this is during a function call to gather up arguments, this is mostly
447/// allocated on the stack, so we can tolerate a larger number of values stored inline.
448///
449/// Most functions have few parameters, but closures with a lot of captured variables can
450/// potentially have many.  Having a larger inline storage for arguments reduces allocations in
451/// scripts with heavy closure usage.
452///
453/// Under `no_closure`, this type aliases to [`StaticVec`][crate::StaticVec] instead.
454#[expose_under_internals]
455#[cfg(not(feature = "no_closure"))]
456type FnArgsVec<T> = smallvec::SmallVec<[T; FN_ARGS_VEC_INLINE_SIZE]>;
457
458/// Inline arguments storage for function calls.
459/// This type aliases to [`StaticVec`][crate::StaticVec].
460#[expose_under_internals]
461#[cfg(feature = "no_closure")]
462type FnArgsVec<T> = crate::StaticVec<T>;
463
464type SmartString = smartstring::SmartString<smartstring::LazyCompact>;
465
466// Compiler guards against mutually-exclusive feature flags
467
468#[cfg(feature = "no_float")]
469#[cfg(feature = "f32_float")]
470compile_error!("`f32_float` cannot be used with `no_float`");
471
472#[cfg(feature = "only_i32")]
473#[cfg(feature = "only_i64")]
474compile_error!("`only_i32` and `only_i64` cannot be used together");
475
476#[cfg(feature = "no_std")]
477#[cfg(feature = "wasm-bindgen")]
478compile_error!("`wasm-bindgen` cannot be used with `no-std`");
479
480#[cfg(feature = "no_std")]
481#[cfg(feature = "stdweb")]
482compile_error!("`stdweb` cannot be used with `no-std`");
483
484#[cfg(target_family = "wasm")]
485#[cfg(feature = "no_std")]
486compile_error!("`no_std` cannot be used for WASM target");
487
488#[cfg(not(target_family = "wasm"))]
489#[cfg(feature = "wasm-bindgen")]
490compile_error!("`wasm-bindgen` cannot be used for non-WASM target");
491
492#[cfg(not(target_family = "wasm"))]
493#[cfg(feature = "stdweb")]
494compile_error!("`stdweb` cannot be used non-WASM target");
495
496#[cfg(feature = "wasm-bindgen")]
497#[cfg(feature = "stdweb")]
498compile_error!("`wasm-bindgen` and `stdweb` cannot be used together");