Allocate packages with higher functions capacity.

This commit is contained in:
Stephen Chung 2020-05-13 19:39:34 +08:00
parent 30e5e2f034
commit 5a02548ebc
6 changed files with 24 additions and 10 deletions

View File

@ -1,5 +1,4 @@
use rhai::{packages::*, Engine, EvalAltResult, INT}; use rhai::{packages::*, Engine, EvalAltResult, INT};
use std::rc::Rc;
fn main() -> Result<(), Box<EvalAltResult>> { fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new_raw(); let mut engine = Engine::new_raw();

View File

@ -1,12 +1,9 @@
use rhai::{Dynamic, Engine, EvalAltResult, Scope, AST, INT}; use rhai::{Dynamic, Engine, EvalAltResult, Scope, AST};
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
use rhai::OptimizationLevel; use rhai::OptimizationLevel;
use std::{ use std::io::{stdin, stdout, Write};
io::{stdin, stdout, Write},
iter,
};
fn print_error(input: &str, err: EvalAltResult) { fn print_error(input: &str, err: EvalAltResult) {
let lines: Vec<_> = input.trim().split('\n').collect(); let lines: Vec<_> = input.trim().split('\n').collect();

View File

@ -3,7 +3,7 @@ use rhai::{Engine, EvalAltResult};
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
use rhai::OptimizationLevel; use rhai::OptimizationLevel;
use std::{env, fs::File, io::Read, iter, process::exit}; use std::{env, fs::File, io::Read, process::exit};
fn eprint_error(input: &str, err: EvalAltResult) { fn eprint_error(input: &str, err: EvalAltResult) {
fn eprint_line(lines: &[&str], line: usize, pos: usize, err: &str) { fn eprint_line(lines: &[&str], line: usize, pos: usize, err: &str) {

View File

@ -94,6 +94,24 @@ impl Module {
Default::default() Default::default()
} }
/// Create a new module with a specified capacity for native Rust functions.
///
/// # Examples
///
/// ```
/// use rhai::Module;
///
/// let mut module = Module::new();
/// module.set_var("answer", 42_i64);
/// assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);
/// ```
pub fn new_with_capacity(capacity: usize) -> Self {
Self {
functions: HashMap::with_capacity(capacity),
..Default::default()
}
}
/// Does a variable exist in the module? /// Does a variable exist in the module?
/// ///
/// # Examples /// # Examples

View File

@ -17,7 +17,7 @@ fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) -> FuncRetu
if position <= 0 { if position <= 0 {
list.insert(0, Dynamic::from(item)); list.insert(0, Dynamic::from(item));
} else if (position as usize) >= list.len() - 1 { } else if (position as usize) >= list.len() - 1 {
push(list, item); push(list, item)?;
} else { } else {
list.insert(position as usize, Dynamic::from(item)); list.insert(position as usize, Dynamic::from(item));
} }
@ -26,7 +26,7 @@ fn ins<T: Variant + Clone>(list: &mut Array, position: INT, item: T) -> FuncRetu
fn pad<T: Variant + Clone>(list: &mut Array, len: INT, item: T) -> FuncReturn<()> { fn pad<T: Variant + Clone>(list: &mut Array, len: INT, item: T) -> FuncReturn<()> {
if len >= 0 { if len >= 0 {
while list.len() < len as usize { while list.len() < len as usize {
push(list, item.clone()); push(list, item.clone())?;
} }
} }
Ok(()) Ok(())

View File

@ -129,7 +129,7 @@ macro_rules! def_package {
impl $package { impl $package {
pub fn new() -> Self { pub fn new() -> Self {
let mut module = $root::Module::new(); let mut module = $root::Module::new_with_capacity(512);
<Self as $root::packages::Package>::init(&mut module); <Self as $root::packages::Package>::init(&mut module);
Self(module.into()) Self(module.into())
} }