2020-06-20 22:56:56 +08:00
|
|
|
What Rhai Isn't
|
|
|
|
===============
|
2020-06-20 15:57:15 +08:00
|
|
|
|
|
|
|
{{#include ../links.md}}
|
|
|
|
|
|
|
|
Rhai's purpose is to provide a dynamic layer over Rust code, in the same spirit of _zero cost abstractions_.
|
|
|
|
It doesn't attempt to be a new language. For example:
|
|
|
|
|
|
|
|
* No classes. Well, Rust doesn't either. On the other hand...
|
|
|
|
|
|
|
|
* No traits... so it is also not Rust. Do your Rusty stuff in Rust.
|
|
|
|
|
2020-10-12 23:17:22 +08:00
|
|
|
* No structures/records/tuples - define your types in Rust instead; Rhai can seamlessly work with _any Rust type_.
|
2020-06-25 18:07:57 +08:00
|
|
|
|
2020-06-20 15:57:15 +08:00
|
|
|
There is, however, a built-in [object map] type which is adequate for most uses.
|
2020-06-27 17:34:39 +08:00
|
|
|
It is possible to simulate [object-oriented programming (OOP)][OOP] by storing [function pointers]
|
2020-09-30 23:02:01 +08:00
|
|
|
or [closures] in [object map] properties, turning them into _methods_.
|
2020-06-20 15:57:15 +08:00
|
|
|
|
|
|
|
* No first-class functions - Code your functions in Rust instead, and register them with Rhai.
|
|
|
|
|
2020-07-26 10:07:40 +08:00
|
|
|
There is, however, support for simple [function pointers] to allow runtime dispatch by function name.
|
2020-06-25 18:07:57 +08:00
|
|
|
|
2020-06-20 15:57:15 +08:00
|
|
|
* No garbage collection - this should be expected, so...
|
|
|
|
|
2020-09-30 23:02:01 +08:00
|
|
|
* No first-class closures - do your closure magic in Rust instead: [turn a Rhai scripted function into a Rust closure]({{rootUrl}}/engine/call-fn.md).
|
2020-06-20 15:57:15 +08:00
|
|
|
|
2020-09-30 23:02:01 +08:00
|
|
|
There is, however, support for simulated [closures] via [currying] a [function pointer] with
|
|
|
|
captured shared variables.
|
2020-07-26 10:07:40 +08:00
|
|
|
|
2020-09-30 23:02:01 +08:00
|
|
|
* No byte-codes/JIT - Rhai has an AST-walking interpreter which will not win any speed races.
|
|
|
|
The purpose of Rhai is not to be extremely _fast_, but to make it as easy as possible to
|
|
|
|
integrate with native Rust applications.
|
|
|
|
|
|
|
|
* No formal language grammar - Rhai uses a hand-coded lexer, a hand-coded top-down recursive-descent parser
|
2020-10-12 23:17:22 +08:00
|
|
|
for statements, and a hand-coded Pratt parser for expressions.
|
2020-09-30 23:02:01 +08:00
|
|
|
|
|
|
|
This lack of formalism allows the parser itself to be exposed as a service in order to support
|
|
|
|
[disabling keywords/operators][disable keywords and operators], adding [custom operators],
|
|
|
|
and defining [custom syntax].
|
2020-06-20 15:57:15 +08:00
|
|
|
|
2020-07-26 10:07:40 +08:00
|
|
|
|
|
|
|
Do Not Write The Next 4D VR Game in Rhai
|
|
|
|
---------------------------------------
|
|
|
|
|
2020-09-30 23:02:01 +08:00
|
|
|
Due to this intended usage, Rhai deliberately keeps the language simple and small by omitting
|
2020-10-12 23:17:22 +08:00
|
|
|
advanced language features such as classes, inheritance, interfaces, generics,
|
|
|
|
first-class functions/closures, pattern matching, concurrency, byte-codes VM, JIT etc.
|
2020-06-20 15:57:15 +08:00
|
|
|
|
2020-09-30 23:02:01 +08:00
|
|
|
Avoid the temptation to write full-fledge application logic entirely in Rhai -
|
|
|
|
that use case is best fulfilled by more complete languages such as JavaScript or Lua.
|
2020-06-20 15:57:15 +08:00
|
|
|
|
2020-07-26 10:07:40 +08:00
|
|
|
|
|
|
|
Thin Dynamic Wrapper Layer Over Rust Code
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
In actual practice, it is usually best to expose a Rust API into Rhai for scripts to call.
|
|
|
|
|
|
|
|
All the core functionalities should be written in Rust, with Rhai being the dynamic _control_ layer.
|
|
|
|
|
2020-09-30 23:02:01 +08:00
|
|
|
This is similar to some dynamic languages where most of the core functionalities reside in a C/C++
|
|
|
|
standard library.
|
2020-07-26 10:07:40 +08:00
|
|
|
|
|
|
|
Another similar scenario is a web front-end driving back-end services written in a systems language.
|
|
|
|
In this case, JavaScript takes the role of Rhai while the back-end language, well... it can actually also be Rust.
|
|
|
|
Except that Rhai integrates with Rust _much_ more tightly, removing the need for interfaces such
|
|
|
|
as XHR calls and payload encoding such as JSON.
|