Add no_function
feature to disable script-defined functions.
This commit is contained in:
60
src/api.rs
60
src/api.rs
@@ -168,17 +168,25 @@ impl<'e> Engine<'e> {
|
||||
retain_functions: bool,
|
||||
ast: &AST,
|
||||
) -> Result<Dynamic, EvalAltResult> {
|
||||
let AST(statements, functions) = ast;
|
||||
#[cfg(feature = "no_function")]
|
||||
let AST(statements) = ast;
|
||||
|
||||
functions.iter().for_each(|f| {
|
||||
engine.script_functions.insert(
|
||||
FnSpec {
|
||||
name: f.name.clone().into(),
|
||||
args: None,
|
||||
},
|
||||
Arc::new(FnIntExt::Int(f.clone())),
|
||||
);
|
||||
});
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
let statements = {
|
||||
let AST(statements, functions) = ast;
|
||||
|
||||
functions.iter().for_each(|f| {
|
||||
engine.script_functions.insert(
|
||||
FnSpec {
|
||||
name: f.name.clone().into(),
|
||||
args: None,
|
||||
},
|
||||
Arc::new(FnIntExt::Int(f.clone())),
|
||||
);
|
||||
});
|
||||
|
||||
statements
|
||||
};
|
||||
|
||||
let result = statements
|
||||
.iter()
|
||||
@@ -244,16 +252,26 @@ impl<'e> Engine<'e> {
|
||||
|
||||
parse(&mut tokens.peekable(), self.optimize)
|
||||
.map_err(|err| EvalAltResult::ErrorParsing(err))
|
||||
.and_then(|AST(ref statements, ref functions)| {
|
||||
for f in functions {
|
||||
self.script_functions.insert(
|
||||
FnSpec {
|
||||
name: f.name.clone().into(),
|
||||
args: None,
|
||||
},
|
||||
Arc::new(FnIntExt::Int(f.clone())),
|
||||
);
|
||||
}
|
||||
.and_then(|ast| {
|
||||
#[cfg(feature = "no_function")]
|
||||
let AST(statements) = ast;
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
let statements = {
|
||||
let AST(ref statements, ref functions) = ast;
|
||||
|
||||
functions.iter().for_each(|f| {
|
||||
self.script_functions.insert(
|
||||
FnSpec {
|
||||
name: f.name.clone().into(),
|
||||
args: None,
|
||||
},
|
||||
Arc::new(FnIntExt::Int(f.clone())),
|
||||
);
|
||||
});
|
||||
|
||||
statements
|
||||
};
|
||||
|
||||
let val = statements
|
||||
.iter()
|
||||
@@ -275,6 +293,7 @@ impl<'e> Engine<'e> {
|
||||
/// ```rust
|
||||
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
||||
/// # #[cfg(not(feature = "no_stdlib"))]
|
||||
/// # #[cfg(not(feature = "no_function"))]
|
||||
/// # {
|
||||
/// use rhai::Engine;
|
||||
///
|
||||
@@ -289,6 +308,7 @@ impl<'e> Engine<'e> {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
pub fn call_fn<A: FuncArgs, T: Any + Clone>(
|
||||
&mut self,
|
||||
name: &str,
|
||||
|
@@ -4,6 +4,8 @@ use crate::any::{Any, AnyExt, Dynamic, Variant};
|
||||
use crate::parser::{Expr, FnDef, Position, Stmt};
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::scope::Scope;
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
use crate::INT;
|
||||
|
||||
use std::{
|
||||
|
@@ -139,7 +139,10 @@ impl fmt::Debug for Position {
|
||||
}
|
||||
|
||||
/// Compiled AST (abstract syntax tree) of a Rhai script.
|
||||
pub struct AST(pub(crate) Vec<Stmt>, pub(crate) Vec<FnDef<'static>>);
|
||||
pub struct AST(
|
||||
pub(crate) Vec<Stmt>,
|
||||
#[cfg(not(feature = "no_function"))] pub(crate) Vec<FnDef<'static>>,
|
||||
);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FnDef<'a> {
|
||||
@@ -1820,6 +1823,7 @@ fn parse_stmt<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, Parse
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
fn parse_fn<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<FnDef<'static>, ParseError> {
|
||||
let pos = match input.next() {
|
||||
Some((_, tok_pos)) => tok_pos,
|
||||
@@ -1892,11 +1896,14 @@ fn parse_top_level<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
optimize_ast: bool,
|
||||
) -> Result<AST, ParseError> {
|
||||
let mut statements = Vec::new();
|
||||
let mut functions = Vec::new();
|
||||
let mut statements = Vec::<Stmt>::new();
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
let mut functions = Vec::<FnDef>::new();
|
||||
|
||||
while input.peek().is_some() {
|
||||
match input.peek() {
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Some(&(Token::Fn, _)) => functions.push(parse_fn(input)?),
|
||||
_ => statements.push(parse_stmt(input)?),
|
||||
}
|
||||
@@ -1910,6 +1917,7 @@ fn parse_top_level<'a>(
|
||||
return Ok(if optimize_ast {
|
||||
AST(
|
||||
optimize(statements),
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
functions
|
||||
.into_iter()
|
||||
.map(|mut fn_def| {
|
||||
@@ -1920,7 +1928,11 @@ fn parse_top_level<'a>(
|
||||
.collect(),
|
||||
)
|
||||
} else {
|
||||
AST(statements, functions)
|
||||
AST(
|
||||
statements,
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
functions,
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user