Streamline code.

This commit is contained in:
Stephen Chung 2020-06-14 22:44:59 +08:00
parent f26c12b8ea
commit 31d2fa410b
2 changed files with 26 additions and 24 deletions

View File

@ -2207,7 +2207,7 @@ let a = new_ts(); // constructor function
a.field = 500; // property setter a.field = 500; // property setter
a.update(); // method call, 'a' can be modified a.update(); // method call, 'a' can be modified
update(a); // <- this de-sugars to 'a.update()' this if 'a' is a simple variable update(a); // <- this de-sugars to 'a.update()' thus if 'a' is a simple variable
// unlike scripted functions, 'a' can be modified and is not a copy // unlike scripted functions, 'a' can be modified and is not a copy
let array = [ a ]; let array = [ a ];

View File

@ -63,9 +63,9 @@ pub const MAX_FUNCTION_EXPR_DEPTH: usize = 32;
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
pub const MAX_CALL_STACK_DEPTH: usize = usize::MAX; pub const MAX_CALL_STACK_DEPTH: usize = usize::MAX;
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
pub const MAX_EXPR_DEPTH: usize = usize::MAX; pub const MAX_EXPR_DEPTH: usize = 0;
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
pub const MAX_FUNCTION_EXPR_DEPTH: usize = usize::MAX; pub const MAX_FUNCTION_EXPR_DEPTH: usize = 0;
pub const KEYWORD_PRINT: &str = "print"; pub const KEYWORD_PRINT: &str = "print";
pub const KEYWORD_DEBUG: &str = "debug"; pub const KEYWORD_DEBUG: &str = "debug";
@ -1761,11 +1761,7 @@ impl Engine {
_ => unreachable!(), _ => unreachable!(),
}; };
if let Ok(val) = &result { self.check_data_size(result)
self.check_data_size(val)?;
}
result
} }
/// Evaluate a statement /// Evaluate a statement
@ -2034,20 +2030,20 @@ impl Engine {
} }
}; };
if let Ok(val) = &result { self.check_data_size(result)
self.check_data_size(val)?;
} }
result /// Check a result to ensure that the data size is within allowable limit.
} fn check_data_size(
&self,
/// Check a `Dynamic` value to ensure that its size is within allowable limit. result: Result<Dynamic, Box<EvalAltResult>>,
fn check_data_size(&self, value: &Dynamic) -> Result<(), Box<EvalAltResult>> { ) -> Result<Dynamic, Box<EvalAltResult>> {
#[cfg(feature = "unchecked")] #[cfg(feature = "unchecked")]
return Ok(()); return result;
// If no data size limits, just return
if self.max_string_size + self.max_array_size + self.max_map_size == 0 { if self.max_string_size + self.max_array_size + self.max_map_size == 0 {
return Ok(()); return result;
} }
// Recursively calculate the size of a value (especially `Array` and `Map`) // Recursively calculate the size of a value (especially `Array` and `Map`)
@ -2090,16 +2086,22 @@ impl Engine {
} }
} }
match value { match result {
Dynamic(Union::Str(_)) if self.max_string_size > 0 => (), // Simply return all errors
Err(_) => return result,
// String with limit
Ok(Dynamic(Union::Str(_))) if self.max_string_size > 0 => (),
// Array with limit
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
Dynamic(Union::Array(_)) if self.max_array_size > 0 => (), Ok(Dynamic(Union::Array(_))) if self.max_array_size > 0 => (),
// Map with limit
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
Dynamic(Union::Map(_)) if self.max_map_size > 0 => (), Ok(Dynamic(Union::Map(_))) if self.max_map_size > 0 => (),
_ => return Ok(()), // Everything else is simply returned
Ok(_) => return result,
}; };
let (arr, map, s) = calc_size(value); let (arr, map, s) = calc_size(result.as_ref().unwrap());
if s > self.max_string_size { if s > self.max_string_size {
Err(Box::new(EvalAltResult::ErrorDataTooLarge( Err(Box::new(EvalAltResult::ErrorDataTooLarge(
@ -2123,7 +2125,7 @@ impl Engine {
Position::none(), Position::none(),
))) )))
} else { } else {
Ok(()) result
} }
} }