中文
R
NiQin shared an aphorism: 这是极为特殊的历史转折期,物质文明发展到这一步注定了精神(信仰)的缺失,灵魂空虚、物欲横流,人们的精神堕入虚无主义,只能沉浸在金钱物质欲望和肉体感官刺激中,有各种不安和痛苦。多少年轻人也失去了纯真的理想和纯美的爱情。这绝不仅是我们这个星球上的生命的悲剧,而是任何一种生命在文明发展进程中注定的悲哀。生命的意义到底是什么?我们到底该怎样活着? -- 《重大人生启示录 - 前言》

Programming Languages Rhai - Embedded Scripting for Rust No.627bca167e84024c6ea07392 Sticky Alternative URL

NiQin updated at 2022-05-11 22:37:10+08:00

💥 Overview of book & doc

Hits: 190545

Keys/tags: rust rhai embedded-scripting embedded-scripting-for-rust

Rhai is an embedded scripting language and evaluation engine for Rust that gives a safe and easy way to add scripting to any application.

Targets and builds

  • All CPU and O/S targets supported by Rust, including:
    • WebAssembly (WASM)
    • no-std
  • Minimum Rust version 1.57

Standard features

  • Simple language similar to JavaScript+Rust with dynamic typing.
  • Fairly efficient evaluation (1 million iterations in 0.3 sec on a single-core, 2.3 GHz Linux VM).
  • Tight integration with native Rust functions and types, including getters/setters, methods and indexers.
  • Freely pass Rust values into a script as variables/constants via an external Scope - all clonable Rust types are supported; no need to implement any special trait. Or tap directly into the variable resolution process.
  • Built-in support for most common data types including booleans, integers, floating-point numbers (including Decimal), strings, Unicode characters, arrays (including packed byte arrays) and object maps.
  • Easily call a script-defined function from Rust.
  • Relatively little unsafe code (yes there are some for performance reasons).
  • Few dependencies - currently only smallvec, num-traits, ahash, bitflags and smartstring.
  • Re-entrant scripting engine can be made Send + Sync (via the sync feature).
  • Compile once to AST form for repeated evaluations.
  • Scripts are optimized (useful for template-based machine-generated scripts).
  • Easy custom API development via plugins system powered by procedural macros.
  • Function overloading and operator overloading.
  • Dynamic dispatch via function pointers with additional support for currying.
  • Closures (anonymous functions) that can capture shared values.
  • Some syntactic support for object-oriented programming (OOP).
  • Organize code base with dynamically-loadable modules, optionally overriding the resolution process.
  • Serialization/deserialization support via serde (requires the serde feature).
  • Support for minimal builds by excluding unneeded language features.
  • A debugging interface.

Protected against attacks

  • Don’t Panic guarantee - Any panic is a bug. Rhai subscribes to the motto that a library should never panic the host system, and is coded with this in mind.
  • Sand-boxed - the scripting engine, if declared immutable, cannot mutate the containing environment unless explicitly permitted.
  • Rugged - protected against malicious attacks (such as stack-overflow, over-sized data, and runaway scripts etc.) that may come from untrusted third-party user-land scripts.
  • Track script evaluation progress and manually terminate a script run.
  • Passes Miri.

For those who actually want their own language

  • Use as a DSL.
  • Disable certain language features such as looping.
  • Further restrict the language by surgically disabling keywords and operators.
  • Define custom operators.
  • Extend the language with custom syntax.
All rights of books and documents belong to the authors, editors, and translators, the goal of this site is to facilitate reading. If there is anything improper, please contact ask@rusthub.org.

Read Now Alternative URL