Merge pull request #328 from schungx/master
Add Engine::compile_to_self_contained API.
This commit is contained in:
commit
1589204335
29
.github/workflows/benchmark.yml
vendored
Normal file
29
.github/workflows/benchmark.yml
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
name: Benchmark
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
benchmark:
|
||||||
|
name: Run Rust benchmark
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- run: rustup toolchain update nightly && rustup default nightly
|
||||||
|
- name: Run benchmark
|
||||||
|
run: cargo +nightly bench | tee output.txt
|
||||||
|
- name: Store benchmark result
|
||||||
|
uses: rhysd/github-action-benchmark@v1
|
||||||
|
with:
|
||||||
|
name: Rust Benchmark
|
||||||
|
tool: 'cargo'
|
||||||
|
output-file-path: output.txt
|
||||||
|
# Use personal access token instead of GITHUB_TOKEN due to https://github.community/t5/GitHub-Actions/Github-action-not-triggering-gh-pages-upon-push/td-p/26869/highlight/false
|
||||||
|
github-token: ${{ secrets.RHAI }}
|
||||||
|
auto-push: true
|
||||||
|
# Show alert with commit comment on detecting possible performance regression
|
||||||
|
alert-threshold: '200%'
|
||||||
|
comment-on-alert: true
|
||||||
|
fail-on-alert: true
|
||||||
|
alert-comment-cc-users: '@schungx'
|
@ -11,6 +11,11 @@ Breaking changes
|
|||||||
* `ParseErrorType::WrongFnDefinition` is renamed `FnWrongDefinition`.
|
* `ParseErrorType::WrongFnDefinition` is renamed `FnWrongDefinition`.
|
||||||
* Redefining an existing function within the same script now throws a new `ParseErrorType::FnDuplicatedDefinition`. This is to prevent accidental overwriting an earlier function definition.
|
* Redefining an existing function within the same script now throws a new `ParseErrorType::FnDuplicatedDefinition`. This is to prevent accidental overwriting an earlier function definition.
|
||||||
|
|
||||||
|
New features
|
||||||
|
------------
|
||||||
|
|
||||||
|
* `Engine::compile_to_self_contained` compiles a script into an `AST` and _eagerly_ resolves all `import` statements with string literal paths. The resolved modules are directly embedded into the `AST`. When the `AST` is later evaluated, `import` statements directly yield the pre-resolved modules without going through the resolution process once again.
|
||||||
|
|
||||||
Enhancements
|
Enhancements
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
120
src/ast.rs
120
src/ast.rs
@ -170,6 +170,9 @@ pub struct AST {
|
|||||||
statements: Vec<Stmt>,
|
statements: Vec<Stmt>,
|
||||||
/// Script-defined functions.
|
/// Script-defined functions.
|
||||||
functions: Shared<Module>,
|
functions: Shared<Module>,
|
||||||
|
/// Embedded module resolver, if any.
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AST {
|
impl Default for AST {
|
||||||
@ -179,6 +182,8 @@ impl Default for AST {
|
|||||||
source: None,
|
source: None,
|
||||||
statements: Vec::with_capacity(16),
|
statements: Vec::with_capacity(16),
|
||||||
functions: Default::default(),
|
functions: Default::default(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -194,6 +199,8 @@ impl AST {
|
|||||||
source: None,
|
source: None,
|
||||||
statements: statements.into_iter().collect(),
|
statements: statements.into_iter().collect(),
|
||||||
functions: functions.into(),
|
functions: functions.into(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Create a new [`AST`] with a source name.
|
/// Create a new [`AST`] with a source name.
|
||||||
@ -207,6 +214,8 @@ impl AST {
|
|||||||
source: Some(source.into()),
|
source: Some(source.into()),
|
||||||
statements: statements.into_iter().collect(),
|
statements: statements.into_iter().collect(),
|
||||||
functions: functions.into(),
|
functions: functions.into(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Get the source.
|
/// Get the source.
|
||||||
@ -269,6 +278,33 @@ impl AST {
|
|||||||
pub fn lib(&self) -> &Module {
|
pub fn lib(&self) -> &Module {
|
||||||
&self.functions
|
&self.functions
|
||||||
}
|
}
|
||||||
|
/// Get the embedded [module resolver][`ModuleResolver`].
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
#[cfg(not(feature = "internals"))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn resolver(
|
||||||
|
&self,
|
||||||
|
) -> Option<Shared<crate::module::resolvers::StaticModuleResolver>> {
|
||||||
|
self.resolver.clone()
|
||||||
|
}
|
||||||
|
/// _(INTERNALS)_ Get the embedded [module resolver][`ModuleResolver`].
|
||||||
|
/// Exported under the `internals` feature only.
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
#[cfg(feature = "internals")]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn resolver(&self) -> Option<Shared<crate::module::resolvers::StaticModuleResolver>> {
|
||||||
|
self.resolver.clone()
|
||||||
|
}
|
||||||
|
/// Set the embedded [module resolver][`ModuleResolver`].
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn set_resolver(
|
||||||
|
&mut self,
|
||||||
|
resolver: impl Into<Shared<crate::module::resolvers::StaticModuleResolver>>,
|
||||||
|
) -> &mut Self {
|
||||||
|
self.resolver = Some(resolver.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
/// Clone the [`AST`]'s functions into a new [`AST`].
|
/// Clone the [`AST`]'s functions into a new [`AST`].
|
||||||
/// No statements are cloned.
|
/// No statements are cloned.
|
||||||
///
|
///
|
||||||
@ -298,6 +334,8 @@ impl AST {
|
|||||||
source: self.source.clone(),
|
source: self.source.clone(),
|
||||||
statements: Default::default(),
|
statements: Default::default(),
|
||||||
functions: functions.into(),
|
functions: functions.into(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: self.resolver.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Clone the [`AST`]'s script statements into a new [`AST`].
|
/// Clone the [`AST`]'s script statements into a new [`AST`].
|
||||||
@ -308,6 +346,8 @@ impl AST {
|
|||||||
source: self.source.clone(),
|
source: self.source.clone(),
|
||||||
statements: self.statements.clone(),
|
statements: self.statements.clone(),
|
||||||
functions: Default::default(),
|
functions: Default::default(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: self.resolver.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Merge two [`AST`] into one. Both [`AST`]'s are untouched and a new, merged, version
|
/// Merge two [`AST`] into one. Both [`AST`]'s are untouched and a new, merged, version
|
||||||
@ -604,6 +644,17 @@ impl AST {
|
|||||||
///
|
///
|
||||||
/// Not available under [`no_function`].
|
/// Not available under [`no_function`].
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn iter_fn_def(&self) -> impl Iterator<Item = &ScriptFnDef> {
|
||||||
|
self.functions
|
||||||
|
.iter_script_fn()
|
||||||
|
.map(|(_, _, _, _, fn_def)| fn_def)
|
||||||
|
}
|
||||||
|
/// Iterate through all function definitions.
|
||||||
|
///
|
||||||
|
/// Not available under [`no_function`].
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn iter_functions<'a>(&'a self) -> impl Iterator<Item = ScriptFnMetadata> + 'a {
|
pub fn iter_functions<'a>(&'a self) -> impl Iterator<Item = ScriptFnMetadata> + 'a {
|
||||||
self.functions
|
self.functions
|
||||||
@ -888,6 +939,53 @@ impl Stmt {
|
|||||||
Self::Share(_) => false,
|
Self::Share(_) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Recursively walk this statement.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn walk(&self, process_stmt: &mut impl FnMut(&Stmt), process_expr: &mut impl FnMut(&Expr)) {
|
||||||
|
process_stmt(self);
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Self::Let(_, Some(e), _, _) | Self::Const(_, Some(e), _, _) => {
|
||||||
|
e.walk(process_stmt, process_expr)
|
||||||
|
}
|
||||||
|
Self::If(e, x, _) => {
|
||||||
|
e.walk(process_stmt, process_expr);
|
||||||
|
x.0.walk(process_stmt, process_expr);
|
||||||
|
if let Some(ref s) = x.1 {
|
||||||
|
s.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self::Switch(e, x, _) => {
|
||||||
|
e.walk(process_stmt, process_expr);
|
||||||
|
x.0.values()
|
||||||
|
.for_each(|s| s.walk(process_stmt, process_expr));
|
||||||
|
if let Some(ref s) = x.1 {
|
||||||
|
s.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self::While(e, s, _) | Self::Do(s, e, _, _) => {
|
||||||
|
e.walk(process_stmt, process_expr);
|
||||||
|
s.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
Self::For(e, x, _) => {
|
||||||
|
e.walk(process_stmt, process_expr);
|
||||||
|
x.1.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
Self::Assignment(x, _) => {
|
||||||
|
x.0.walk(process_stmt, process_expr);
|
||||||
|
x.2.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
Self::Block(x, _) => x.iter().for_each(|s| s.walk(process_stmt, process_expr)),
|
||||||
|
Self::TryCatch(x, _, _) => {
|
||||||
|
x.0.walk(process_stmt, process_expr);
|
||||||
|
x.2.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
Self::Expr(e) | Self::Return(_, Some(e), _) => e.walk(process_stmt, process_expr),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
Self::Import(e, _, _) => e.walk(process_stmt, process_expr),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// _(INTERNALS)_ A custom syntax definition.
|
/// _(INTERNALS)_ A custom syntax definition.
|
||||||
@ -1308,6 +1406,28 @@ impl Expr {
|
|||||||
Self::Custom(_, _) => false,
|
Self::Custom(_, _) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Recursively walk this expression.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn walk(&self, process_stmt: &mut impl FnMut(&Stmt), process_expr: &mut impl FnMut(&Expr)) {
|
||||||
|
process_expr(self);
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Self::Stmt(x, _) => x.iter().for_each(|s| s.walk(process_stmt, process_expr)),
|
||||||
|
Self::Array(x, _) => x.iter().for_each(|e| e.walk(process_stmt, process_expr)),
|
||||||
|
Self::Map(x, _) => x
|
||||||
|
.iter()
|
||||||
|
.for_each(|(_, e)| e.walk(process_stmt, process_expr)),
|
||||||
|
Self::Index(x, _) | Expr::In(x, _) | Expr::And(x, _) | Expr::Or(x, _) => {
|
||||||
|
x.lhs.walk(process_stmt, process_expr);
|
||||||
|
x.rhs.walk(process_stmt, process_expr);
|
||||||
|
}
|
||||||
|
Self::Custom(x, _) => x
|
||||||
|
.keywords
|
||||||
|
.iter()
|
||||||
|
.for_each(|e| e.walk(process_stmt, process_expr)),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -516,6 +516,9 @@ pub struct State {
|
|||||||
pub operations: u64,
|
pub operations: u64,
|
||||||
/// Number of modules loaded.
|
/// Number of modules loaded.
|
||||||
pub modules: usize,
|
pub modules: usize,
|
||||||
|
/// Embedded module resolver.
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
pub resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>,
|
||||||
/// Cached lookup values for function hashes.
|
/// Cached lookup values for function hashes.
|
||||||
pub functions_cache: HashMap<
|
pub functions_cache: HashMap<
|
||||||
NonZeroU64,
|
NonZeroU64,
|
||||||
@ -2328,7 +2331,21 @@ impl Engine {
|
|||||||
.eval_expr(scope, mods, state, lib, this_ptr, &expr, level)?
|
.eval_expr(scope, mods, state, lib, this_ptr, &expr, level)?
|
||||||
.try_cast::<ImmutableString>()
|
.try_cast::<ImmutableString>()
|
||||||
{
|
{
|
||||||
let module = self.module_resolver.resolve(self, &path, expr.position())?;
|
use crate::ModuleResolver;
|
||||||
|
|
||||||
|
let expr_pos = expr.position();
|
||||||
|
|
||||||
|
let module = state
|
||||||
|
.resolver
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|r| match r.resolve(self, &path, expr_pos) {
|
||||||
|
Ok(m) => return Some(Ok(m)),
|
||||||
|
Err(err) => match *err {
|
||||||
|
EvalAltResult::ErrorModuleNotFound(_, _) => None,
|
||||||
|
_ => return Some(Err(err)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| self.module_resolver.resolve(self, &path, expr_pos))?;
|
||||||
|
|
||||||
if let Some(name_def) = alias {
|
if let Some(name_def) = alias {
|
||||||
if !module.is_indexed() {
|
if !module.is_indexed() {
|
||||||
|
@ -889,6 +889,68 @@ impl Engine {
|
|||||||
pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result<AST, ParseError> {
|
pub fn compile_with_scope(&self, scope: &Scope, script: &str) -> Result<AST, ParseError> {
|
||||||
self.compile_scripts_with_scope(scope, &[script])
|
self.compile_scripts_with_scope(scope, &[script])
|
||||||
}
|
}
|
||||||
|
/// Compile a string into an [`AST`] using own scope, which can be used later for evaluation,
|
||||||
|
/// embedding all imported modules.
|
||||||
|
///
|
||||||
|
/// Modules referred by `import` statements containing literal string paths are eagerly resolved
|
||||||
|
/// via the current [module resolver][crate::ModuleResolver] and embedded into the resultant
|
||||||
|
/// [`AST`]. When it is evaluated later, `import` statement directly recall pre-resolved
|
||||||
|
/// [modules][Module] and the resolution process is not performed again.
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
pub fn compile_into_self_contained(
|
||||||
|
&self,
|
||||||
|
scope: &Scope,
|
||||||
|
script: &str,
|
||||||
|
) -> Result<AST, Box<EvalAltResult>> {
|
||||||
|
use crate::{
|
||||||
|
ast::{Expr, Stmt},
|
||||||
|
fn_native::shared_take_or_clone,
|
||||||
|
module::resolvers::StaticModuleResolver,
|
||||||
|
stdlib::collections::HashMap,
|
||||||
|
ImmutableString,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut ast = self.compile_scripts_with_scope(scope, &[script])?;
|
||||||
|
let mut imports = HashMap::<ImmutableString, Position>::new();
|
||||||
|
|
||||||
|
ast.statements()
|
||||||
|
.iter()
|
||||||
|
.chain({
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
{
|
||||||
|
ast.iter_fn_def().map(|f| &f.body)
|
||||||
|
}
|
||||||
|
#[cfg(feature = "no_function")]
|
||||||
|
{
|
||||||
|
crate::stdlib::iter::empty()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.for_each(|stmt| {
|
||||||
|
stmt.walk(
|
||||||
|
&mut |stmt| match stmt {
|
||||||
|
Stmt::Import(Expr::StringConstant(s, pos), _, _)
|
||||||
|
if !imports.contains_key(s) =>
|
||||||
|
{
|
||||||
|
imports.insert(s.clone(), *pos);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
&mut |_| {},
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
if !imports.is_empty() {
|
||||||
|
let mut resolver = StaticModuleResolver::new();
|
||||||
|
for (path, pos) in imports {
|
||||||
|
let module = self.module_resolver.resolve(self, &path, pos)?;
|
||||||
|
let module = shared_take_or_clone(module);
|
||||||
|
resolver.insert(path, module);
|
||||||
|
}
|
||||||
|
ast.set_resolver(resolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ast)
|
||||||
|
}
|
||||||
/// When passed a list of strings, first join the strings into one large script,
|
/// When passed a list of strings, first join the strings into one large script,
|
||||||
/// and then compile them into an [`AST`] using own scope, which can be used later for evaluation.
|
/// and then compile them into an [`AST`] using own scope, which can be used later for evaluation.
|
||||||
///
|
///
|
||||||
@ -1457,6 +1519,8 @@ impl Engine {
|
|||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let state = &mut State {
|
let state = &mut State {
|
||||||
source: ast.clone_source(),
|
source: ast.clone_source(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: ast.resolver(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
self.eval_statements_raw(scope, mods, state, ast.statements(), &[ast.lib()], level)
|
self.eval_statements_raw(scope, mods, state, ast.statements(), &[ast.lib()], level)
|
||||||
@ -1524,6 +1588,8 @@ impl Engine {
|
|||||||
let mods = &mut (&self.global_sub_modules).into();
|
let mods = &mut (&self.global_sub_modules).into();
|
||||||
let state = &mut State {
|
let state = &mut State {
|
||||||
source: ast.clone_source(),
|
source: ast.clone_source(),
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
resolver: ast.resolver(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
self.eval_statements_raw(scope, mods, state, ast.statements(), &[ast.lib()], 0)?;
|
self.eval_statements_raw(scope, mods, state, ast.statements(), &[ast.lib()], 0)?;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
#![cfg(not(feature = "no_module"))]
|
#![cfg(not(feature = "no_module"))]
|
||||||
use rhai::{
|
use rhai::{
|
||||||
module_resolvers::StaticModuleResolver, Dynamic, Engine, EvalAltResult, FnNamespace,
|
module_resolvers::{DummyModuleResolver, StaticModuleResolver},
|
||||||
ImmutableString, Module, ParseError, ParseErrorType, Scope, INT,
|
Dynamic, Engine, EvalAltResult, FnNamespace, ImmutableString, Module, ParseError,
|
||||||
|
ParseErrorType, Scope, INT,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -246,6 +247,20 @@ fn test_module_resolver() -> Result<(), Box<EvalAltResult>> {
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let script = r#"
|
||||||
|
import "hello" as h;
|
||||||
|
h::answer
|
||||||
|
"#;
|
||||||
|
let mut scope = Scope::new();
|
||||||
|
|
||||||
|
let ast = engine.compile_into_self_contained(&mut scope, script)?;
|
||||||
|
|
||||||
|
engine.set_module_resolver(DummyModuleResolver::new());
|
||||||
|
|
||||||
|
assert_eq!(engine.eval_ast::<INT>(&ast)?, 42);
|
||||||
|
|
||||||
|
assert!(engine.eval::<INT>(script).is_err());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user