Streamline code and feature gates.
This commit is contained in:
parent
8d0623d07f
commit
60891e694f
17
src/any.rs
17
src/any.rs
@ -270,9 +270,9 @@ impl Dynamic {
|
|||||||
|
|
||||||
/// Does this `Dynamic` hold a shared data type
|
/// Does this `Dynamic` hold a shared data type
|
||||||
/// instead of one of the support system primitive types?
|
/// instead of one of the support system primitive types?
|
||||||
#[cfg(not(feature = "no_shared"))]
|
|
||||||
pub fn is_shared(&self) -> bool {
|
pub fn is_shared(&self) -> bool {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
|
#[cfg(not(feature = "no_shared"))]
|
||||||
Union::Shared(_) => true,
|
Union::Shared(_) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
@ -440,7 +440,7 @@ impl Clone for Dynamic {
|
|||||||
Union::FnPtr(ref value) => Self(Union::FnPtr(value.clone())),
|
Union::FnPtr(ref value) => Self(Union::FnPtr(value.clone())),
|
||||||
Union::Variant(ref value) => (***value).clone_into_dynamic(),
|
Union::Variant(ref value) => (***value).clone_into_dynamic(),
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
Union::Shared(ref cell) => Self(Union::Shared(Box::new((**cell).clone()))),
|
Union::Shared(ref cell) => Self(Union::Shared(cell.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -561,9 +561,13 @@ impl Dynamic {
|
|||||||
/// Shared `Dynamic` values can be converted seamlessly to and from ordinary `Dynamic` values.
|
/// Shared `Dynamic` values can be converted seamlessly to and from ordinary `Dynamic` values.
|
||||||
///
|
///
|
||||||
/// If the `Dynamic` value is already shared, this method returns itself.
|
/// If the `Dynamic` value is already shared, this method returns itself.
|
||||||
#[cfg(not(feature = "no_shared"))]
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics under the `no_shared` feature.
|
||||||
pub fn into_shared(self) -> Self {
|
pub fn into_shared(self) -> Self {
|
||||||
match self.0 {
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
return match self.0 {
|
||||||
Union::Shared(..) => self,
|
Union::Shared(..) => self,
|
||||||
_ => Self(Union::Shared(Box::new(SharedCell {
|
_ => Self(Union::Shared(Box::new(SharedCell {
|
||||||
value_type_id: self.type_id(),
|
value_type_id: self.type_id(),
|
||||||
@ -574,7 +578,10 @@ impl Dynamic {
|
|||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
container: Arc::new(RwLock::new(self)),
|
container: Arc::new(RwLock::new(self)),
|
||||||
}))),
|
}))),
|
||||||
}
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "no_shared")]
|
||||||
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert the `Dynamic` value into specific type.
|
/// Convert the `Dynamic` value into specific type.
|
||||||
|
13
src/api.rs
13
src/api.rs
@ -1305,16 +1305,15 @@ impl Engine {
|
|||||||
mut ast: AST,
|
mut ast: AST,
|
||||||
optimization_level: OptimizationLevel,
|
optimization_level: OptimizationLevel,
|
||||||
) -> AST {
|
) -> AST {
|
||||||
#[cfg(not(feature = "no_function"))]
|
let lib = if cfg!(not(feature = "no_function")) {
|
||||||
let lib = ast
|
ast.lib()
|
||||||
.lib()
|
|
||||||
.iter_fn()
|
.iter_fn()
|
||||||
.filter(|(_, _, _, f)| f.is_script())
|
.filter(|(_, _, _, f)| f.is_script())
|
||||||
.map(|(_, _, _, f)| f.get_fn_def().clone())
|
.map(|(_, _, _, f)| f.get_fn_def().clone())
|
||||||
.collect();
|
.collect()
|
||||||
|
} else {
|
||||||
#[cfg(feature = "no_function")]
|
Default::default()
|
||||||
let lib = Default::default();
|
};
|
||||||
|
|
||||||
let stmt = mem::take(ast.statements_mut());
|
let stmt = mem::take(ast.statements_mut());
|
||||||
optimize_into_ast(self, scope, stmt, lib, optimization_level)
|
optimize_into_ast(self, scope, stmt, lib, optimization_level)
|
||||||
|
@ -32,6 +32,7 @@ use crate::module::resolvers;
|
|||||||
use crate::utils::ImmutableString;
|
use crate::utils::ImmutableString;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
use crate::any::DynamicWriteLock;
|
use crate::any::DynamicWriteLock;
|
||||||
|
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
@ -44,12 +45,13 @@ use crate::stdlib::{
|
|||||||
vec::Vec,
|
vec::Vec,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
use crate::stdlib::ops::DerefMut;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
use crate::stdlib::any::TypeId;
|
use crate::stdlib::any::TypeId;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_shared"))]
|
|
||||||
use crate::stdlib::ops::DerefMut;
|
|
||||||
|
|
||||||
/// Variable-sized array of `Dynamic` values.
|
/// Variable-sized array of `Dynamic` values.
|
||||||
///
|
///
|
||||||
/// Not available under the `no_index` feature.
|
/// Not available under the `no_index` feature.
|
||||||
@ -134,6 +136,7 @@ pub enum Target<'a> {
|
|||||||
/// The target is a mutable reference to a Shared `Dynamic` value.
|
/// The target is a mutable reference to a Shared `Dynamic` value.
|
||||||
/// It holds both the access guard and the original shared value.
|
/// It holds both the access guard and the original shared value.
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
LockGuard((DynamicWriteLock<'a, Dynamic>, Dynamic)),
|
LockGuard((DynamicWriteLock<'a, Dynamic>, Dynamic)),
|
||||||
/// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects).
|
/// The target is a temporary `Dynamic` value (i.e. the mutation can cause no side effects).
|
||||||
Value(Dynamic),
|
Value(Dynamic),
|
||||||
@ -150,6 +153,7 @@ impl Target<'_> {
|
|||||||
match self {
|
match self {
|
||||||
Self::Ref(_) => true,
|
Self::Ref(_) => true,
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
Self::LockGuard(_) => true,
|
Self::LockGuard(_) => true,
|
||||||
Self::Value(_) => false,
|
Self::Value(_) => false,
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -161,18 +165,32 @@ impl Target<'_> {
|
|||||||
match self {
|
match self {
|
||||||
Self::Ref(_) => false,
|
Self::Ref(_) => false,
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
Self::LockGuard(_) => false,
|
Self::LockGuard(_) => false,
|
||||||
Self::Value(_) => true,
|
Self::Value(_) => true,
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
Self::StringChar(_, _, _) => false,
|
Self::StringChar(_, _, _) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Is the `Target` a shared value?
|
||||||
|
pub fn is_shared(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Ref(r) => r.is_shared(),
|
||||||
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
Self::LockGuard(_) => true,
|
||||||
|
Self::Value(r) => r.is_shared(),
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
Self::StringChar(_, _, _) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Is the `Target` a specific type?
|
/// Is the `Target` a specific type?
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn is<T: Variant + Clone>(&self) -> bool {
|
pub fn is<T: Variant + Clone>(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Target::Ref(r) => r.is::<T>(),
|
Target::Ref(r) => r.is::<T>(),
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
Target::LockGuard((r, _)) => r.is::<T>(),
|
Target::LockGuard((r, _)) => r.is::<T>(),
|
||||||
Target::Value(r) => r.is::<T>(),
|
Target::Value(r) => r.is::<T>(),
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -184,6 +202,7 @@ impl Target<'_> {
|
|||||||
match self {
|
match self {
|
||||||
Self::Ref(r) => r.clone(), // Referenced value is cloned
|
Self::Ref(r) => r.clone(), // Referenced value is cloned
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
Self::LockGuard((_, orig)) => orig, // Original value is simply taken
|
Self::LockGuard((_, orig)) => orig, // Original value is simply taken
|
||||||
Self::Value(v) => v, // Owned value is simply taken
|
Self::Value(v) => v, // Owned value is simply taken
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -195,6 +214,7 @@ impl Target<'_> {
|
|||||||
match self {
|
match self {
|
||||||
Self::Ref(r) => *r,
|
Self::Ref(r) => *r,
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
Self::LockGuard((r, _)) => r.deref_mut(),
|
Self::LockGuard((r, _)) => r.deref_mut(),
|
||||||
Self::Value(ref mut r) => r,
|
Self::Value(ref mut r) => r,
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
@ -207,6 +227,7 @@ impl Target<'_> {
|
|||||||
match self {
|
match self {
|
||||||
Self::Ref(r) => **r = new_val,
|
Self::Ref(r) => **r = new_val,
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
Self::LockGuard((r, _)) => **r = new_val,
|
Self::LockGuard((r, _)) => **r = new_val,
|
||||||
Self::Value(_) => {
|
Self::Value(_) => {
|
||||||
return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
||||||
@ -243,6 +264,7 @@ impl Target<'_> {
|
|||||||
impl<'a> From<&'a mut Dynamic> for Target<'a> {
|
impl<'a> From<&'a mut Dynamic> for Target<'a> {
|
||||||
fn from(value: &'a mut Dynamic) -> Self {
|
fn from(value: &'a mut Dynamic) -> Self {
|
||||||
#[cfg(not(feature = "no_shared"))]
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
if value.is_shared() {
|
if value.is_shared() {
|
||||||
// Cloning is cheap for a shared value
|
// Cloning is cheap for a shared value
|
||||||
let container = value.clone();
|
let container = value.clone();
|
||||||
@ -427,11 +449,11 @@ impl Default for Engine {
|
|||||||
progress: None,
|
progress: None,
|
||||||
|
|
||||||
// optimization level
|
// optimization level
|
||||||
#[cfg(feature = "no_optimize")]
|
optimization_level: if cfg!(feature = "no_optimize") {
|
||||||
optimization_level: OptimizationLevel::None,
|
OptimizationLevel::None
|
||||||
|
} else {
|
||||||
#[cfg(not(feature = "no_optimize"))]
|
OptimizationLevel::Simple
|
||||||
optimization_level: OptimizationLevel::Simple,
|
},
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
limits: Limits {
|
limits: Limits {
|
||||||
@ -639,11 +661,11 @@ impl Engine {
|
|||||||
debug: Box::new(|_| {}),
|
debug: Box::new(|_| {}),
|
||||||
progress: None,
|
progress: None,
|
||||||
|
|
||||||
#[cfg(feature = "no_optimize")]
|
optimization_level: if cfg!(feature = "no_optimize") {
|
||||||
optimization_level: OptimizationLevel::None,
|
OptimizationLevel::None
|
||||||
|
} else {
|
||||||
#[cfg(not(feature = "no_optimize"))]
|
OptimizationLevel::Simple
|
||||||
optimization_level: OptimizationLevel::Simple,
|
},
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
limits: Limits {
|
limits: Limits {
|
||||||
@ -1340,16 +1362,11 @@ impl Engine {
|
|||||||
)),
|
)),
|
||||||
// Normal assignment
|
// Normal assignment
|
||||||
ScopeEntryType::Normal if op.is_empty() => {
|
ScopeEntryType::Normal if op.is_empty() => {
|
||||||
#[cfg(not(feature = "no_shared"))]
|
if cfg!(not(feature = "no_shared")) && lhs_ptr.is_shared() {
|
||||||
if lhs_ptr.is_shared() {
|
|
||||||
*lhs_ptr.write_lock::<Dynamic>().unwrap() = rhs_val;
|
*lhs_ptr.write_lock::<Dynamic>().unwrap() = rhs_val;
|
||||||
} else {
|
} else {
|
||||||
*lhs_ptr = rhs_val;
|
*lhs_ptr = rhs_val;
|
||||||
}
|
}
|
||||||
#[cfg(feature = "no_shared")]
|
|
||||||
{
|
|
||||||
*lhs_ptr = rhs_val;
|
|
||||||
}
|
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
// Op-assignment - in order of precedence:
|
// Op-assignment - in order of precedence:
|
||||||
@ -1394,16 +1411,11 @@ impl Engine {
|
|||||||
)
|
)
|
||||||
.map_err(|err| err.new_position(*op_pos))?;
|
.map_err(|err| err.new_position(*op_pos))?;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_shared"))]
|
if cfg!(not(feature = "no_shared")) && lhs_ptr.is_shared() {
|
||||||
if lhs_ptr.is_shared() {
|
|
||||||
*lhs_ptr.write_lock::<Dynamic>().unwrap() = value;
|
*lhs_ptr.write_lock::<Dynamic>().unwrap() = value;
|
||||||
} else {
|
} else {
|
||||||
*lhs_ptr = value;
|
*lhs_ptr = value;
|
||||||
}
|
}
|
||||||
#[cfg(feature = "no_shared")]
|
|
||||||
{
|
|
||||||
*lhs_ptr = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(Default::default())
|
Ok(Default::default())
|
||||||
}
|
}
|
||||||
@ -1845,13 +1857,15 @@ impl Engine {
|
|||||||
.map_err(|err| err.new_position(stmt.position()))
|
.map_err(|err| err.new_position(stmt.position()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check a result to ensure that the data size is within allowable limit.
|
||||||
|
/// Position in `EvalAltResult` may be None and should be set afterwards.
|
||||||
#[cfg(feature = "unchecked")]
|
#[cfg(feature = "unchecked")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn check_data_size(
|
fn check_data_size(
|
||||||
&self,
|
&self,
|
||||||
result: Result<Dynamic, Box<EvalAltResult>>,
|
result: Result<Dynamic, Box<EvalAltResult>>,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
return result;
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check a result to ensure that the data size is within allowable limit.
|
/// Check a result to ensure that the data size is within allowable limit.
|
||||||
@ -1861,9 +1875,6 @@ impl Engine {
|
|||||||
&self,
|
&self,
|
||||||
result: Result<Dynamic, Box<EvalAltResult>>,
|
result: Result<Dynamic, Box<EvalAltResult>>,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
#[cfg(feature = "unchecked")]
|
|
||||||
return result;
|
|
||||||
|
|
||||||
// If no data size limits, just return
|
// If no data size limits, just return
|
||||||
if self.limits.max_string_size + self.limits.max_array_size + self.limits.max_map_size == 0
|
if self.limits.max_string_size + self.limits.max_array_size + self.limits.max_map_size == 0
|
||||||
{
|
{
|
||||||
|
@ -20,9 +20,8 @@ use crate::utils::StaticVec;
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
use crate::{
|
use crate::{
|
||||||
parser::ScriptFnDef,
|
parser::ScriptFnDef, r#unsafe::unsafe_cast_var_name_to_lifetime,
|
||||||
r#unsafe::unsafe_cast_var_name_to_lifetime,
|
scope::EntryType as ScopeEntryType,
|
||||||
scope::{Entry as ScopeEntry, EntryType as ScopeEntryType},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
@ -34,18 +33,23 @@ use crate::engine::{FN_IDX_GET, FN_IDX_SET};
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
use crate::engine::{Map, Target, FN_GET, FN_SET};
|
use crate::engine::{Map, Target, FN_GET, FN_SET};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
use crate::scope::Entry as ScopeEntry;
|
||||||
|
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{
|
||||||
any::{type_name, TypeId},
|
any::{type_name, TypeId},
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
collections::HashSet,
|
|
||||||
convert::TryFrom,
|
convert::TryFrom,
|
||||||
format,
|
format,
|
||||||
iter::{empty, once},
|
iter::{empty, once},
|
||||||
mem,
|
mem,
|
||||||
string::{String, ToString},
|
string::ToString,
|
||||||
vec::Vec,
|
vec::Vec,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
use crate::stdlib::{collections::HashSet, string::String};
|
||||||
|
|
||||||
/// Extract the property name from a getter function name.
|
/// Extract the property name from a getter function name.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn extract_prop_from_getter(_fn_name: &str) -> Option<&str> {
|
fn extract_prop_from_getter(_fn_name: &str) -> Option<&str> {
|
||||||
@ -422,7 +426,7 @@ impl Engine {
|
|||||||
is_ref: bool,
|
is_ref: bool,
|
||||||
is_method: bool,
|
is_method: bool,
|
||||||
pub_only: bool,
|
pub_only: bool,
|
||||||
capture: Option<Scope>,
|
_capture: Option<Scope>,
|
||||||
def_val: Option<bool>,
|
def_val: Option<bool>,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||||
@ -451,12 +455,6 @@ impl Engine {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fn
|
|
||||||
KEYWORD_IS_SHARED if args.len() == 1 => Err(Box::new(EvalAltResult::ErrorRuntime(
|
|
||||||
"'is_shared' should not be called in method style. Try is_shared(...);".into(),
|
|
||||||
Position::none(),
|
|
||||||
))),
|
|
||||||
|
|
||||||
// eval - reaching this point it must be a method-style call
|
// eval - reaching this point it must be a method-style call
|
||||||
KEYWORD_EVAL
|
KEYWORD_EVAL
|
||||||
if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) =>
|
if args.len() == 1 && !self.has_override(lib, hash_fn, hash_script, pub_only) =>
|
||||||
@ -478,7 +476,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Add captured variables into scope
|
// Add captured variables into scope
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
if let Some(captured) = capture {
|
if let Some(captured) = _capture {
|
||||||
add_captured_variables_into_scope(&func.externals, captured, scope);
|
add_captured_variables_into_scope(&func.externals, captured, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -662,22 +660,18 @@ impl Engine {
|
|||||||
.into(),
|
.into(),
|
||||||
false,
|
false,
|
||||||
))
|
))
|
||||||
} else if _fn_name == KEYWORD_SHARED && idx.is_empty() {
|
} else if cfg!(not(feature = "no_shared"))
|
||||||
// take call
|
&& _fn_name == KEYWORD_IS_SHARED
|
||||||
#[cfg(not(feature = "no_shared"))]
|
&& idx.is_empty()
|
||||||
{
|
{
|
||||||
|
// take call
|
||||||
|
Ok((target.is_shared().into(), false))
|
||||||
|
} else if cfg!(not(feature = "no_shared")) && _fn_name == KEYWORD_SHARED && idx.is_empty() {
|
||||||
|
// take call
|
||||||
Ok((obj.clone().into_shared(), false))
|
Ok((obj.clone().into_shared(), false))
|
||||||
}
|
} else if cfg!(not(feature = "no_shared")) && _fn_name == KEYWORD_TAKE && idx.is_empty() {
|
||||||
#[cfg(feature = "no_shared")]
|
|
||||||
unreachable!()
|
|
||||||
} else if _fn_name == KEYWORD_TAKE && idx.is_empty() {
|
|
||||||
// take call
|
// take call
|
||||||
#[cfg(not(feature = "no_shared"))]
|
|
||||||
{
|
|
||||||
Ok((obj.clone_inner_data::<Dynamic>().unwrap(), false))
|
Ok((obj.clone_inner_data::<Dynamic>().unwrap(), false))
|
||||||
}
|
|
||||||
#[cfg(feature = "no_shared")]
|
|
||||||
unreachable!()
|
|
||||||
} else {
|
} else {
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
let redirected;
|
let redirected;
|
||||||
@ -790,8 +784,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle is_shared()
|
// Handle is_shared()
|
||||||
#[cfg(not(feature = "no_shared"))]
|
if cfg!(not(feature = "no_shared")) && name == KEYWORD_IS_SHARED && args_expr.len() == 1 {
|
||||||
if name == KEYWORD_IS_SHARED && args_expr.len() == 1 {
|
|
||||||
let expr = args_expr.get(0).unwrap();
|
let expr = args_expr.get(0).unwrap();
|
||||||
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
||||||
|
|
||||||
@ -799,8 +792,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle shared()
|
// Handle shared()
|
||||||
#[cfg(not(feature = "no_shared"))]
|
if cfg!(not(feature = "no_shared")) && name == KEYWORD_SHARED && args_expr.len() == 1 {
|
||||||
if name == KEYWORD_SHARED && args_expr.len() == 1 {
|
|
||||||
let expr = args_expr.get(0).unwrap();
|
let expr = args_expr.get(0).unwrap();
|
||||||
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
||||||
|
|
||||||
@ -808,8 +800,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle take()
|
// Handle take()
|
||||||
#[cfg(not(feature = "no_shared"))]
|
if cfg!(not(feature = "no_shared")) && name == KEYWORD_TAKE && args_expr.len() == 1 {
|
||||||
if name == KEYWORD_TAKE && args_expr.len() == 1 {
|
|
||||||
let expr = args_expr.get(0).unwrap();
|
let expr = args_expr.get(0).unwrap();
|
||||||
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
let value = self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?;
|
||||||
|
|
||||||
@ -875,7 +866,7 @@ impl Engine {
|
|||||||
let mut arg_values: StaticVec<_>;
|
let mut arg_values: StaticVec<_>;
|
||||||
let mut args: StaticVec<_>;
|
let mut args: StaticVec<_>;
|
||||||
let mut is_ref = false;
|
let mut is_ref = false;
|
||||||
let capture = if capture && !scope.is_empty() {
|
let capture = if cfg!(not(feature = "no_capture")) && capture && !scope.is_empty() {
|
||||||
Some(scope.flatten_clone())
|
Some(scope.flatten_clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -943,18 +934,10 @@ impl Engine {
|
|||||||
args_expr: &[Expr],
|
args_expr: &[Expr],
|
||||||
def_val: Option<bool>,
|
def_val: Option<bool>,
|
||||||
hash_script: u64,
|
hash_script: u64,
|
||||||
capture: bool,
|
_capture: bool,
|
||||||
level: usize,
|
level: usize,
|
||||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||||
let modules = modules.as_ref().unwrap();
|
let modules = modules.as_ref().unwrap();
|
||||||
|
|
||||||
#[cfg(not(feature = "no_capture"))]
|
|
||||||
let capture = if capture && !scope.is_empty() {
|
|
||||||
Some(scope.flatten_clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut arg_values: StaticVec<_>;
|
let mut arg_values: StaticVec<_>;
|
||||||
let mut args: StaticVec<_>;
|
let mut args: StaticVec<_>;
|
||||||
|
|
||||||
@ -1027,8 +1010,12 @@ impl Engine {
|
|||||||
|
|
||||||
// Add captured variables into scope
|
// Add captured variables into scope
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
if let Some(captured) = capture {
|
if _capture && !scope.is_empty() {
|
||||||
add_captured_variables_into_scope(&func.externals, captured, scope);
|
add_captured_variables_into_scope(
|
||||||
|
&func.externals,
|
||||||
|
scope.flatten_clone(),
|
||||||
|
scope,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.call_script_fn(scope, mods, state, lib, &mut None, name, func, args, level)
|
self.call_script_fn(scope, mods, state, lib, &mut None, name, func, args, level)
|
||||||
@ -1073,7 +1060,7 @@ pub fn run_builtin_binary_op(
|
|||||||
let x = x.clone().cast::<INT>();
|
let x = x.clone().cast::<INT>();
|
||||||
let y = y.clone().cast::<INT>();
|
let y = y.clone().cast::<INT>();
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
if cfg!(not(feature = "unchecked")) {
|
||||||
match op {
|
match op {
|
||||||
"+" => return add(x, y).map(Into::into).map(Some),
|
"+" => return add(x, y).map(Into::into).map(Some),
|
||||||
"-" => return sub(x, y).map(Into::into).map(Some),
|
"-" => return sub(x, y).map(Into::into).map(Some),
|
||||||
@ -1085,8 +1072,7 @@ pub fn run_builtin_binary_op(
|
|||||||
"<<" => return shl(x, y).map(Into::into).map(Some),
|
"<<" => return shl(x, y).map(Into::into).map(Some),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
#[cfg(feature = "unchecked")]
|
|
||||||
match op {
|
match op {
|
||||||
"+" => return Ok(Some((x + y).into())),
|
"+" => return Ok(Some((x + y).into())),
|
||||||
"-" => return Ok(Some((x - y).into())),
|
"-" => return Ok(Some((x - y).into())),
|
||||||
@ -1098,6 +1084,7 @@ pub fn run_builtin_binary_op(
|
|||||||
"<<" => return shl_u(x, y).map(Into::into).map(Some),
|
"<<" => return shl_u(x, y).map(Into::into).map(Some),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
"==" => return Ok(Some((x == y).into())),
|
"==" => return Ok(Some((x == y).into())),
|
||||||
|
@ -576,17 +576,13 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
|
|||||||
|
|
||||||
// First search in functions lib (can override built-in)
|
// First search in functions lib (can override built-in)
|
||||||
// Cater for both normal function call style and method call style (one additional arguments)
|
// Cater for both normal function call style and method call style (one additional arguments)
|
||||||
#[cfg(not(feature = "no_function"))]
|
let has_script_fn = cfg!(not(feature = "no_function")) && state.lib.iter_fn().find(|(_, _, _, f)| {
|
||||||
let _has_script_fn = state.lib.iter_fn().find(|(_, _, _, f)| {
|
|
||||||
if !f.is_script() { return false; }
|
if !f.is_script() { return false; }
|
||||||
let fn_def = f.get_fn_def();
|
let fn_def = f.get_fn_def();
|
||||||
fn_def.name == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len())
|
fn_def.name == name && (args.len()..=args.len() + 1).contains(&fn_def.params.len())
|
||||||
}).is_some();
|
}).is_some();
|
||||||
|
|
||||||
#[cfg(feature = "no_function")]
|
if has_script_fn {
|
||||||
let _has_script_fn: bool = false;
|
|
||||||
|
|
||||||
if _has_script_fn {
|
|
||||||
// A script-defined function overrides the built-in function - do not make the call
|
// A script-defined function overrides the built-in function - do not make the call
|
||||||
x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect();
|
x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect();
|
||||||
return Expr::FnCall(x);
|
return Expr::FnCall(x);
|
||||||
@ -748,11 +744,13 @@ pub fn optimize_into_ast(
|
|||||||
_functions: Vec<ScriptFnDef>,
|
_functions: Vec<ScriptFnDef>,
|
||||||
level: OptimizationLevel,
|
level: OptimizationLevel,
|
||||||
) -> AST {
|
) -> AST {
|
||||||
#[cfg(feature = "no_optimize")]
|
let level = if cfg!(feature = "no_optimize") {
|
||||||
const level: OptimizationLevel = OptimizationLevel::None;
|
OptimizationLevel::None
|
||||||
|
} else {
|
||||||
|
level
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
let lib = if cfg!(not(feature = "no_function")) {
|
||||||
let lib = {
|
|
||||||
let mut module = Module::new();
|
let mut module = Module::new();
|
||||||
|
|
||||||
if !level.is_none() {
|
if !level.is_none() {
|
||||||
@ -814,11 +812,10 @@ pub fn optimize_into_ast(
|
|||||||
}
|
}
|
||||||
|
|
||||||
module
|
module
|
||||||
|
} else {
|
||||||
|
Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "no_function")]
|
|
||||||
let lib = Default::default();
|
|
||||||
|
|
||||||
AST::new(
|
AST::new(
|
||||||
match level {
|
match level {
|
||||||
OptimizationLevel::None => statements,
|
OptimizationLevel::None => statements,
|
||||||
|
@ -2,38 +2,29 @@ use crate::def_package;
|
|||||||
use crate::module::FuncReturn;
|
use crate::module::FuncReturn;
|
||||||
use crate::parser::INT;
|
use crate::parser::INT;
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
use crate::{result::EvalAltResult, token::Position};
|
use crate::{result::EvalAltResult, token::Position};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
use crate::parser::FLOAT;
|
use crate::parser::FLOAT;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
|
||||||
#[cfg(feature = "no_std")]
|
|
||||||
use num_traits::*;
|
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
use num_traits::{
|
use num_traits::{
|
||||||
identities::Zero, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl,
|
identities::Zero, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl,
|
||||||
CheckedShr, CheckedSub,
|
CheckedShr, CheckedSub,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
#[cfg(feature = "no_std")]
|
||||||
#[cfg(not(feature = "only_i64"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
use crate::stdlib::ops::{BitAnd, BitOr, BitXor};
|
use num_traits::float::Float;
|
||||||
|
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
use crate::stdlib::{
|
||||||
use crate::stdlib::ops::{Add, Div, Mul, Neg, Rem, Sub};
|
boxed::Box,
|
||||||
|
fmt::Display,
|
||||||
#[cfg(feature = "unchecked")]
|
format,
|
||||||
use crate::stdlib::ops::{Shl, Shr};
|
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},
|
||||||
|
};
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
use crate::stdlib::{boxed::Box, fmt::Display, format};
|
|
||||||
|
|
||||||
// Checked add
|
// Checked add
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn add<T: Display + CheckedAdd>(x: T, y: T) -> FuncReturn<T> {
|
||||||
pub(crate) fn add<T: Display + CheckedAdd>(x: T, y: T) -> FuncReturn<T> {
|
|
||||||
x.checked_add(&y).ok_or_else(|| {
|
x.checked_add(&y).ok_or_else(|| {
|
||||||
Box::new(EvalAltResult::ErrorArithmetic(
|
Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Addition overflow: {} + {}", x, y),
|
format!("Addition overflow: {} + {}", x, y),
|
||||||
@ -42,8 +33,7 @@ pub(crate) fn add<T: Display + CheckedAdd>(x: T, y: T) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Checked subtract
|
// Checked subtract
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn sub<T: Display + CheckedSub>(x: T, y: T) -> FuncReturn<T> {
|
||||||
pub(crate) fn sub<T: Display + CheckedSub>(x: T, y: T) -> FuncReturn<T> {
|
|
||||||
x.checked_sub(&y).ok_or_else(|| {
|
x.checked_sub(&y).ok_or_else(|| {
|
||||||
Box::new(EvalAltResult::ErrorArithmetic(
|
Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Subtraction underflow: {} - {}", x, y),
|
format!("Subtraction underflow: {} - {}", x, y),
|
||||||
@ -52,8 +42,7 @@ pub(crate) fn sub<T: Display + CheckedSub>(x: T, y: T) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Checked multiply
|
// Checked multiply
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn mul<T: Display + CheckedMul>(x: T, y: T) -> FuncReturn<T> {
|
||||||
pub(crate) fn mul<T: Display + CheckedMul>(x: T, y: T) -> FuncReturn<T> {
|
|
||||||
x.checked_mul(&y).ok_or_else(|| {
|
x.checked_mul(&y).ok_or_else(|| {
|
||||||
Box::new(EvalAltResult::ErrorArithmetic(
|
Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Multiplication overflow: {} * {}", x, y),
|
format!("Multiplication overflow: {} * {}", x, y),
|
||||||
@ -62,8 +51,7 @@ pub(crate) fn mul<T: Display + CheckedMul>(x: T, y: T) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Checked divide
|
// Checked divide
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn div<T>(x: T, y: T) -> FuncReturn<T>
|
||||||
pub(crate) fn div<T>(x: T, y: T) -> FuncReturn<T>
|
|
||||||
where
|
where
|
||||||
T: Display + CheckedDiv + PartialEq + Zero,
|
T: Display + CheckedDiv + PartialEq + Zero,
|
||||||
{
|
{
|
||||||
@ -83,8 +71,7 @@ where
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Checked negative - e.g. -(i32::MIN) will overflow i32::MAX
|
// Checked negative - e.g. -(i32::MIN) will overflow i32::MAX
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn neg<T: Display + CheckedNeg>(x: T) -> FuncReturn<T> {
|
||||||
pub(crate) fn neg<T: Display + CheckedNeg>(x: T) -> FuncReturn<T> {
|
|
||||||
x.checked_neg().ok_or_else(|| {
|
x.checked_neg().ok_or_else(|| {
|
||||||
Box::new(EvalAltResult::ErrorArithmetic(
|
Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Negation overflow: -{}", x),
|
format!("Negation overflow: -{}", x),
|
||||||
@ -93,8 +80,7 @@ pub(crate) fn neg<T: Display + CheckedNeg>(x: T) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Checked absolute
|
// Checked absolute
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn abs<T: Display + CheckedNeg + PartialOrd + Zero>(x: T) -> FuncReturn<T> {
|
||||||
pub(crate) fn abs<T: Display + CheckedNeg + PartialOrd + Zero>(x: T) -> FuncReturn<T> {
|
|
||||||
// FIX - We don't use Signed::abs() here because, contrary to documentation, it panics
|
// FIX - We don't use Signed::abs() here because, contrary to documentation, it panics
|
||||||
// when the number is ::MIN instead of returning ::MIN itself.
|
// when the number is ::MIN instead of returning ::MIN itself.
|
||||||
if x >= <T as Zero>::zero() {
|
if x >= <T as Zero>::zero() {
|
||||||
@ -109,32 +95,26 @@ pub(crate) fn abs<T: Display + CheckedNeg + PartialOrd + Zero>(x: T) -> FuncRetu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unchecked add - may panic on overflow
|
// Unchecked add - may panic on overflow
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn add_u<T: Add>(x: T, y: T) -> FuncReturn<<T as Add>::Output> {
|
fn add_u<T: Add>(x: T, y: T) -> FuncReturn<<T as Add>::Output> {
|
||||||
Ok(x + y)
|
Ok(x + y)
|
||||||
}
|
}
|
||||||
// Unchecked subtract - may panic on underflow
|
// Unchecked subtract - may panic on underflow
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn sub_u<T: Sub>(x: T, y: T) -> FuncReturn<<T as Sub>::Output> {
|
fn sub_u<T: Sub>(x: T, y: T) -> FuncReturn<<T as Sub>::Output> {
|
||||||
Ok(x - y)
|
Ok(x - y)
|
||||||
}
|
}
|
||||||
// Unchecked multiply - may panic on overflow
|
// Unchecked multiply - may panic on overflow
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn mul_u<T: Mul>(x: T, y: T) -> FuncReturn<<T as Mul>::Output> {
|
fn mul_u<T: Mul>(x: T, y: T) -> FuncReturn<<T as Mul>::Output> {
|
||||||
Ok(x * y)
|
Ok(x * y)
|
||||||
}
|
}
|
||||||
// Unchecked divide - may panic when dividing by zero
|
// Unchecked divide - may panic when dividing by zero
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn div_u<T: Div>(x: T, y: T) -> FuncReturn<<T as Div>::Output> {
|
fn div_u<T: Div>(x: T, y: T) -> FuncReturn<<T as Div>::Output> {
|
||||||
Ok(x / y)
|
Ok(x / y)
|
||||||
}
|
}
|
||||||
// Unchecked negative - may panic on overflow
|
// Unchecked negative - may panic on overflow
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn neg_u<T: Neg>(x: T) -> FuncReturn<<T as Neg>::Output> {
|
fn neg_u<T: Neg>(x: T) -> FuncReturn<<T as Neg>::Output> {
|
||||||
Ok(-x)
|
Ok(-x)
|
||||||
}
|
}
|
||||||
// Unchecked absolute - may panic on overflow
|
// Unchecked absolute - may panic on overflow
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn abs_u<T>(x: T) -> FuncReturn<<T as Neg>::Output>
|
fn abs_u<T>(x: T) -> FuncReturn<<T as Neg>::Output>
|
||||||
where
|
where
|
||||||
T: Neg + PartialOrd + Default + Into<<T as Neg>::Output>,
|
T: Neg + PartialOrd + Default + Into<<T as Neg>::Output>,
|
||||||
@ -147,24 +127,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Bit operators
|
// Bit operators
|
||||||
#[cfg(not(feature = "only_i32"))]
|
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
fn binary_and<T: BitAnd>(x: T, y: T) -> FuncReturn<<T as BitAnd>::Output> {
|
fn binary_and<T: BitAnd>(x: T, y: T) -> FuncReturn<<T as BitAnd>::Output> {
|
||||||
Ok(x & y)
|
Ok(x & y)
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "only_i32"))]
|
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
fn binary_or<T: BitOr>(x: T, y: T) -> FuncReturn<<T as BitOr>::Output> {
|
fn binary_or<T: BitOr>(x: T, y: T) -> FuncReturn<<T as BitOr>::Output> {
|
||||||
Ok(x | y)
|
Ok(x | y)
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "only_i32"))]
|
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
fn binary_xor<T: BitXor>(x: T, y: T) -> FuncReturn<<T as BitXor>::Output> {
|
fn binary_xor<T: BitXor>(x: T, y: T) -> FuncReturn<<T as BitXor>::Output> {
|
||||||
Ok(x ^ y)
|
Ok(x ^ y)
|
||||||
}
|
}
|
||||||
// Checked left-shift
|
// Checked left-shift
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn shl<T: Display + CheckedShl>(x: T, y: INT) -> FuncReturn<T> {
|
||||||
pub(crate) fn shl<T: Display + CheckedShl>(x: T, y: INT) -> FuncReturn<T> {
|
|
||||||
// Cannot shift by a negative number of bits
|
// Cannot shift by a negative number of bits
|
||||||
if y < 0 {
|
if y < 0 {
|
||||||
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
@ -181,8 +154,7 @@ pub(crate) fn shl<T: Display + CheckedShl>(x: T, y: INT) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Checked right-shift
|
// Checked right-shift
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn shr<T: Display + CheckedShr>(x: T, y: INT) -> FuncReturn<T> {
|
||||||
pub(crate) fn shr<T: Display + CheckedShr>(x: T, y: INT) -> FuncReturn<T> {
|
|
||||||
// Cannot shift by a negative number of bits
|
// Cannot shift by a negative number of bits
|
||||||
if y < 0 {
|
if y < 0 {
|
||||||
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
@ -199,18 +171,15 @@ pub(crate) fn shr<T: Display + CheckedShr>(x: T, y: INT) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Unchecked left-shift - may panic if shifting by a negative number of bits
|
// Unchecked left-shift - may panic if shifting by a negative number of bits
|
||||||
#[cfg(feature = "unchecked")]
|
pub fn shl_u<T: Shl<T>>(x: T, y: T) -> FuncReturn<<T as Shl<T>>::Output> {
|
||||||
pub(crate) fn shl_u<T: Shl<T>>(x: T, y: T) -> FuncReturn<<T as Shl<T>>::Output> {
|
|
||||||
Ok(x.shl(y))
|
Ok(x.shl(y))
|
||||||
}
|
}
|
||||||
// Unchecked right-shift - may panic if shifting by a negative number of bits
|
// Unchecked right-shift - may panic if shifting by a negative number of bits
|
||||||
#[cfg(feature = "unchecked")]
|
pub fn shr_u<T: Shr<T>>(x: T, y: T) -> FuncReturn<<T as Shr<T>>::Output> {
|
||||||
pub(crate) fn shr_u<T: Shr<T>>(x: T, y: T) -> FuncReturn<<T as Shr<T>>::Output> {
|
|
||||||
Ok(x.shr(y))
|
Ok(x.shr(y))
|
||||||
}
|
}
|
||||||
// Checked modulo
|
// Checked modulo
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn modulo<T: Display + CheckedRem>(x: T, y: T) -> FuncReturn<T> {
|
||||||
pub(crate) fn modulo<T: Display + CheckedRem>(x: T, y: T) -> FuncReturn<T> {
|
|
||||||
x.checked_rem(&y).ok_or_else(|| {
|
x.checked_rem(&y).ok_or_else(|| {
|
||||||
Box::new(EvalAltResult::ErrorArithmetic(
|
Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Modulo division by zero or overflow: {} % {}", x, y),
|
format!("Modulo division by zero or overflow: {} % {}", x, y),
|
||||||
@ -219,14 +188,12 @@ pub(crate) fn modulo<T: Display + CheckedRem>(x: T, y: T) -> FuncReturn<T> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Unchecked modulo - may panic if dividing by zero
|
// Unchecked modulo - may panic if dividing by zero
|
||||||
#[cfg(any(feature = "unchecked", not(feature = "no_float")))]
|
|
||||||
fn modulo_u<T: Rem>(x: T, y: T) -> FuncReturn<<T as Rem>::Output> {
|
fn modulo_u<T: Rem>(x: T, y: T) -> FuncReturn<<T as Rem>::Output> {
|
||||||
Ok(x % y)
|
Ok(x % y)
|
||||||
}
|
}
|
||||||
// Checked power
|
// Checked power
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn pow_i_i(x: INT, y: INT) -> FuncReturn<INT> {
|
||||||
pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn<INT> {
|
if cfg!(not(feature = "only_i32")) {
|
||||||
#[cfg(not(feature = "only_i32"))]
|
|
||||||
if y > (u32::MAX as INT) {
|
if y > (u32::MAX as INT) {
|
||||||
Err(Box::new(EvalAltResult::ErrorArithmetic(
|
Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Integer raised to too large an index: {} ~ {}", x, y),
|
format!("Integer raised to too large an index: {} ~ {}", x, y),
|
||||||
@ -245,8 +212,7 @@ pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn<INT> {
|
|||||||
))
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
#[cfg(feature = "only_i32")]
|
|
||||||
if y < 0 {
|
if y < 0 {
|
||||||
Err(Box::new(EvalAltResult::ErrorArithmetic(
|
Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
format!("Integer raised to a negative index: {} ~ {}", x, y),
|
format!("Integer raised to a negative index: {} ~ {}", x, y),
|
||||||
@ -261,20 +227,19 @@ pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn<INT> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX)
|
// Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX)
|
||||||
#[cfg(feature = "unchecked")]
|
pub fn pow_i_i_u(x: INT, y: INT) -> FuncReturn<INT> {
|
||||||
pub(crate) fn pow_i_i_u(x: INT, y: INT) -> FuncReturn<INT> {
|
|
||||||
Ok(x.pow(y as u32))
|
Ok(x.pow(y as u32))
|
||||||
}
|
}
|
||||||
// Floating-point power - always well-defined
|
// Floating-point power - always well-defined
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
pub(crate) fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn<FLOAT> {
|
pub fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn<FLOAT> {
|
||||||
Ok(x.powf(y))
|
Ok(x.powf(y))
|
||||||
}
|
}
|
||||||
// Checked power
|
// Checked power
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
#[cfg(not(feature = "unchecked"))]
|
pub fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
|
||||||
pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
|
|
||||||
// Raise to power that is larger than an i32
|
// Raise to power that is larger than an i32
|
||||||
if y > (i32::MAX as INT) {
|
if y > (i32::MAX as INT) {
|
||||||
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
return Err(Box::new(EvalAltResult::ErrorArithmetic(
|
||||||
@ -286,9 +251,8 @@ pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
|
|||||||
Ok(x.powi(y as i32))
|
Ok(x.powi(y as i32))
|
||||||
}
|
}
|
||||||
// Unchecked power - may be incorrect if the power index is too high (> i32::MAX)
|
// Unchecked power - may be incorrect if the power index is too high (> i32::MAX)
|
||||||
#[cfg(feature = "unchecked")]
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
pub(crate) fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
|
pub fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
|
||||||
Ok(x.powi(y as i32))
|
Ok(x.powi(y as i32))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,11 +281,8 @@ macro_rules! reg_sign {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
if cfg!(not(feature = "unchecked")) {
|
||||||
{
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
{
|
|
||||||
// Checked basic arithmetic
|
// Checked basic arithmetic
|
||||||
reg_op!(lib, "+", add, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "+", add, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, u32, u64);
|
||||||
@ -332,8 +293,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "+", add, i128, u128);
|
reg_op!(lib, "+", add, i128, u128);
|
||||||
reg_op!(lib, "-", sub, i128, u128);
|
reg_op!(lib, "-", sub, i128, u128);
|
||||||
reg_op!(lib, "*", mul, i128, u128);
|
reg_op!(lib, "*", mul, i128, u128);
|
||||||
@ -345,8 +305,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unchecked")]
|
if cfg!(feature = "unchecked") {
|
||||||
{
|
|
||||||
// Unchecked basic arithmetic
|
// Unchecked basic arithmetic
|
||||||
reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64);
|
||||||
@ -357,8 +316,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "+", add_u, i128, u128);
|
reg_op!(lib, "+", add_u, i128, u128);
|
||||||
reg_op!(lib, "-", sub_u, i128, u128);
|
reg_op!(lib, "-", sub_u, i128, u128);
|
||||||
reg_op!(lib, "*", mul_u, i128, u128);
|
reg_op!(lib, "*", mul_u, i128, u128);
|
||||||
@ -372,13 +330,13 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
|
|
||||||
reg_sign!(lib, "sign", INT, i8, i16, i32, i64);
|
reg_sign!(lib, "sign", INT, i8, i16, i32, i64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
reg_sign!(lib, "sign", INT, i128);
|
reg_sign!(lib, "sign", INT, i128);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Basic arithmetic for floating-point - no need to check
|
// Basic arithmetic for floating-point - no need to check
|
||||||
#[cfg(not(feature = "no_float"))]
|
if cfg!(not(feature = "no_float")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "+", add_u, f32);
|
reg_op!(lib, "+", add_u, f32);
|
||||||
reg_op!(lib, "-", sub_u, f32);
|
reg_op!(lib, "-", sub_u, f32);
|
||||||
reg_op!(lib, "*", mul_u, f32);
|
reg_op!(lib, "*", mul_u, f32);
|
||||||
@ -387,15 +345,12 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
reg_sign!(lib, "sign", f64, f64);
|
reg_sign!(lib, "sign", f64, f64);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "|", binary_or, i128, u128);
|
reg_op!(lib, "|", binary_or, i128, u128);
|
||||||
reg_op!(lib, "&", binary_and, i128, u128);
|
reg_op!(lib, "&", binary_and, i128, u128);
|
||||||
reg_op!(lib, "^", binary_xor, i128, u128);
|
reg_op!(lib, "^", binary_xor, i128, u128);
|
||||||
@ -405,12 +360,11 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
{
|
{
|
||||||
// Checked power
|
// Checked power
|
||||||
#[cfg(not(feature = "unchecked"))]
|
if cfg!(not(feature = "unchecked")) {
|
||||||
lib.set_fn_2("~", pow_f_i);
|
lib.set_fn_2("~", pow_f_i);
|
||||||
|
} else {
|
||||||
// Unchecked power
|
|
||||||
#[cfg(feature = "unchecked")]
|
|
||||||
lib.set_fn_2("~", pow_f_i_u);
|
lib.set_fn_2("~", pow_f_i_u);
|
||||||
|
}
|
||||||
|
|
||||||
// Floating-point modulo and power
|
// Floating-point modulo and power
|
||||||
reg_op!(lib, "%", modulo_u, f32);
|
reg_op!(lib, "%", modulo_u, f32);
|
||||||
@ -421,19 +375,15 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Checked unary
|
// Checked unary
|
||||||
#[cfg(not(feature = "unchecked"))]
|
if cfg!(not(feature = "unchecked")) {
|
||||||
{
|
|
||||||
reg_unary!(lib, "-", neg, INT);
|
reg_unary!(lib, "-", neg, INT);
|
||||||
reg_unary!(lib, "abs", abs, INT);
|
reg_unary!(lib, "abs", abs, INT);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_unary!(lib, "-", neg, i8, i16, i32, i64);
|
reg_unary!(lib, "-", neg, i8, i16, i32, i64);
|
||||||
reg_unary!(lib, "abs", abs, i8, i16, i32, i64);
|
reg_unary!(lib, "abs", abs, i8, i16, i32, i64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_unary!(lib, "-", neg, i128);
|
reg_unary!(lib, "-", neg, i128);
|
||||||
reg_unary!(lib, "abs", abs, i128);
|
reg_unary!(lib, "abs", abs, i128);
|
||||||
}
|
}
|
||||||
@ -441,19 +391,15 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Unchecked unary
|
// Unchecked unary
|
||||||
#[cfg(feature = "unchecked")]
|
if cfg!(feature = "unchecked") {
|
||||||
{
|
|
||||||
reg_unary!(lib, "-", neg_u, INT);
|
reg_unary!(lib, "-", neg_u, INT);
|
||||||
reg_unary!(lib, "abs", abs_u, INT);
|
reg_unary!(lib, "abs", abs_u, INT);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_unary!(lib, "-", neg_u, i8, i16, i32, i64);
|
reg_unary!(lib, "-", neg_u, i8, i16, i32, i64);
|
||||||
reg_unary!(lib, "abs", abs_u, i8, i16, i32, i64);
|
reg_unary!(lib, "abs", abs_u, i8, i16, i32, i64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_unary!(lib, "-", neg_u, i128);
|
reg_unary!(lib, "-", neg_u, i128);
|
||||||
reg_unary!(lib, "abs", abs_u, i128);
|
reg_unary!(lib, "abs", abs_u, i128);
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,6 @@ macro_rules! reg_pad {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
||||||
reg_op!(lib, "push", push, INT, bool, char, ImmutableString, Array, ());
|
reg_op!(lib, "push", push, INT, bool, char, ImmutableString, Array, ());
|
||||||
reg_pad!(lib, "pad", pad, INT, bool, char, ImmutableString, Array, ());
|
reg_pad!(lib, "pad", pad, INT, bool, char, ImmutableString, Array, ());
|
||||||
@ -104,15 +103,12 @@ def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_op!(lib, "push", push, i8, u8, i16, u16, i32, i64, u32, u64);
|
reg_op!(lib, "push", push, i8, u8, i16, u16, i32, i64, u32, u64);
|
||||||
reg_pad!(lib, "pad", pad, i8, u8, i16, u16, i32, u32, i64, u64);
|
reg_pad!(lib, "pad", pad, i8, u8, i16, u16, i32, u32, i64, u64);
|
||||||
reg_tri!(lib, "insert", ins, i8, u8, i16, u16, i32, i64, u32, u64);
|
reg_tri!(lib, "insert", ins, i8, u8, i16, u16, i32, i64, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "push", push, i128, u128);
|
reg_op!(lib, "push", push, i128, u128);
|
||||||
reg_pad!(lib, "pad", pad, i128, u128);
|
reg_pad!(lib, "pad", pad, i128, u128);
|
||||||
reg_tri!(lib, "insert", ins, i128, u128);
|
reg_tri!(lib, "insert", ins, i128, u128);
|
||||||
|
@ -73,9 +73,7 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
|||||||
reg_range::<INT>(lib);
|
reg_range::<INT>(lib);
|
||||||
lib.set_fn_2("range", get_range::<INT>);
|
lib.set_fn_2("range", get_range::<INT>);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
macro_rules! reg_range {
|
macro_rules! reg_range {
|
||||||
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
||||||
$(
|
$(
|
||||||
@ -87,16 +85,15 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
|||||||
|
|
||||||
reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
|
reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
reg_range!(lib, "range", i128, u128);
|
reg_range!(lib, "range", i128, u128);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
reg_step::<INT>(lib);
|
reg_step::<INT>(lib);
|
||||||
lib.set_fn_3("range", get_step_range::<INT>);
|
lib.set_fn_3("range", get_step_range::<INT>);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
macro_rules! reg_step {
|
macro_rules! reg_step {
|
||||||
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
||||||
$(
|
$(
|
||||||
@ -108,7 +105,8 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
|||||||
|
|
||||||
reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
|
reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
reg_step!(lib, "range", i128, u128);
|
reg_step!(lib, "range", i128, u128);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
@ -33,9 +33,7 @@ macro_rules! reg_op {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def_package!(crate:LogicPackage:"Logical operators.", lib, {
|
def_package!(crate:LogicPackage:"Logical operators.", lib, {
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "<", lt, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "<=", lte, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, ">", gt, i8, u8, i16, u16, i32, u32, u64);
|
||||||
@ -43,8 +41,7 @@ def_package!(crate:LogicPackage:"Logical operators.", lib, {
|
|||||||
reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "==", eq, i8, u8, i16, u16, i32, u32, u64);
|
||||||
reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, u32, u64);
|
reg_op!(lib, "!=", ne, i8, u8, i16, u16, i32, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "<", lt, i128, u128);
|
reg_op!(lib, "<", lt, i128, u128);
|
||||||
reg_op!(lib, "<=", lte, i128, u128);
|
reg_op!(lib, "<=", lte, i128, u128);
|
||||||
reg_op!(lib, ">", gt, i128, u128);
|
reg_op!(lib, ">", gt, i128, u128);
|
||||||
|
@ -1,25 +1,20 @@
|
|||||||
#![cfg(not(feature = "no_object"))]
|
#![cfg(not(feature = "no_object"))]
|
||||||
|
|
||||||
|
use crate::any::Dynamic;
|
||||||
use crate::def_package;
|
use crate::def_package;
|
||||||
use crate::engine::Map;
|
use crate::engine::Map;
|
||||||
|
use crate::module::FuncReturn;
|
||||||
use crate::parser::{ImmutableString, INT};
|
use crate::parser::{ImmutableString, INT};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
use crate::{any::Dynamic, module::FuncReturn};
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
use crate::stdlib::vec::Vec;
|
use crate::stdlib::vec::Vec;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
fn map_get_keys(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
|
fn map_get_keys(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
|
||||||
Ok(map.iter().map(|(k, _)| k.clone().into()).collect())
|
Ok(map.iter().map(|(k, _)| k.clone().into()).collect())
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
|
fn map_get_values(map: &mut Map) -> FuncReturn<Vec<Dynamic>> {
|
||||||
Ok(map.iter().map(|(_, v)| v.clone()).collect())
|
Ok(map.iter().map(|(_, v)| v.clone()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
||||||
lib.set_fn_2_mut(
|
lib.set_fn_2_mut(
|
||||||
"has",
|
"has",
|
||||||
@ -74,9 +69,11 @@ def_package!(crate:BasicMapPackage:"Basic object map utilities.", lib, {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Register map access functions
|
// Register map access functions
|
||||||
#[cfg(not(feature = "no_index"))]
|
if cfg!(not(feature = "no_index")) {
|
||||||
lib.set_fn_1_mut("keys", map_get_keys);
|
lib.set_fn_1_mut("keys", map_get_keys);
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
if cfg!(not(feature = "no_index")) {
|
||||||
lib.set_fn_1_mut("values", map_get_values);
|
lib.set_fn_1_mut("values", map_get_values);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
@ -5,22 +5,20 @@ use crate::parser::INT;
|
|||||||
use crate::parser::FLOAT;
|
use crate::parser::FLOAT;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
use crate::{result::EvalAltResult, token::Position};
|
use crate::{result::EvalAltResult, token::Position};
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use num_traits::*;
|
#[cfg(not(feature = "no_float"))]
|
||||||
|
use num_traits::float::Float;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
use crate::stdlib::{boxed::Box, format};
|
use crate::stdlib::{boxed::Box, format};
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
#[cfg(feature = "only_i32")]
|
#[cfg(feature = "only_i32")]
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
pub const MAX_INT: INT = i32::MAX;
|
pub const MAX_INT: INT = i32::MAX;
|
||||||
|
#[allow(dead_code)]
|
||||||
#[cfg(not(feature = "only_i32"))]
|
#[cfg(not(feature = "only_i32"))]
|
||||||
#[cfg(not(feature = "unchecked"))]
|
|
||||||
pub const MAX_INT: INT = i64::MAX;
|
pub const MAX_INT: INT = i64::MAX;
|
||||||
|
|
||||||
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
||||||
@ -69,9 +67,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
|||||||
lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: INT| Ok(x as FLOAT));
|
||||||
lib.set_fn_1("to_float", |x: f32| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: f32| Ok(x as FLOAT));
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
lib.set_fn_1("to_float", |x: i8| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: i8| Ok(x as FLOAT));
|
||||||
lib.set_fn_1("to_float", |x: u8| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: u8| Ok(x as FLOAT));
|
||||||
lib.set_fn_1("to_float", |x: i16| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: i16| Ok(x as FLOAT));
|
||||||
@ -81,8 +77,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
|||||||
lib.set_fn_1("to_float", |x: i64| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: i64| Ok(x as FLOAT));
|
||||||
lib.set_fn_1("to_float", |x: u64| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: u64| Ok(x as FLOAT));
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
lib.set_fn_1("to_float", |x: i128| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: i128| Ok(x as FLOAT));
|
||||||
lib.set_fn_1("to_float", |x: u128| Ok(x as FLOAT));
|
lib.set_fn_1("to_float", |x: u128| Ok(x as FLOAT));
|
||||||
}
|
}
|
||||||
@ -91,28 +86,25 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
|||||||
|
|
||||||
lib.set_fn_1("to_int", |ch: char| Ok(ch as INT));
|
lib.set_fn_1("to_int", |ch: char| Ok(ch as INT));
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
lib.set_fn_1("to_int", |x: i8| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: i8| Ok(x as INT));
|
||||||
lib.set_fn_1("to_int", |x: u8| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: u8| Ok(x as INT));
|
||||||
lib.set_fn_1("to_int", |x: i16| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: i16| Ok(x as INT));
|
||||||
lib.set_fn_1("to_int", |x: u16| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: u16| Ok(x as INT));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) {
|
||||||
{
|
|
||||||
lib.set_fn_1("to_int", |x: i32| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: i32| Ok(x as INT));
|
||||||
lib.set_fn_1("to_int", |x: u64| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: u64| Ok(x as INT));
|
||||||
|
|
||||||
#[cfg(feature = "only_i64")]
|
if cfg!(feature = "only_i64") {
|
||||||
lib.set_fn_1("to_int", |x: u32| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: u32| Ok(x as INT));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_float"))]
|
#[cfg(not(feature = "no_float"))]
|
||||||
{
|
{
|
||||||
#[cfg(not(feature = "unchecked"))]
|
if cfg!(not(feature = "unchecked")) {
|
||||||
{
|
|
||||||
lib.set_fn_1(
|
lib.set_fn_1(
|
||||||
"to_int",
|
"to_int",
|
||||||
|x: f32| {
|
|x: f32| {
|
||||||
@ -141,8 +133,7 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unchecked")]
|
if cfg!(feature = "unchecked") {
|
||||||
{
|
|
||||||
lib.set_fn_1("to_int", |x: f32| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: f32| Ok(x as INT));
|
||||||
lib.set_fn_1("to_int", |x: f64| Ok(x as INT));
|
lib.set_fn_1("to_int", |x: f64| Ok(x as INT));
|
||||||
}
|
}
|
||||||
|
@ -48,9 +48,7 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
|
|||||||
|
|
||||||
reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString);
|
reg_op!(lib, KEYWORD_DEBUG, to_debug, INT, bool, (), char, ImmutableString);
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_op!(lib, KEYWORD_PRINT, to_string, i8, u8, i16, u16, i32, u32);
|
reg_op!(lib, KEYWORD_PRINT, to_string, i8, u8, i16, u16, i32, u32);
|
||||||
reg_op!(lib, FN_TO_STRING, to_string, i8, u8, i16, u16, i32, u32);
|
reg_op!(lib, FN_TO_STRING, to_string, i8, u8, i16, u16, i32, u32);
|
||||||
reg_op!(lib, KEYWORD_DEBUG, to_debug, i8, u8, i16, u16, i32, u32);
|
reg_op!(lib, KEYWORD_DEBUG, to_debug, i8, u8, i16, u16, i32, u32);
|
||||||
@ -58,8 +56,7 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin
|
|||||||
reg_op!(lib, FN_TO_STRING, to_string, i64, u64);
|
reg_op!(lib, FN_TO_STRING, to_string, i64, u64);
|
||||||
reg_op!(lib, KEYWORD_DEBUG, to_debug, i64, u64);
|
reg_op!(lib, KEYWORD_DEBUG, to_debug, i64, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, KEYWORD_PRINT, to_string, i128, u128);
|
reg_op!(lib, KEYWORD_PRINT, to_string, i128, u128);
|
||||||
reg_op!(lib, FN_TO_STRING, to_string, i128, u128);
|
reg_op!(lib, FN_TO_STRING, to_string, i128, u128);
|
||||||
reg_op!(lib, KEYWORD_DEBUG, to_debug, i128, u128);
|
reg_op!(lib, KEYWORD_DEBUG, to_debug, i128, u128);
|
||||||
|
@ -94,14 +94,11 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
|
|||||||
reg_op!(lib, "+", prepend, INT, bool, char);
|
reg_op!(lib, "+", prepend, INT, bool, char);
|
||||||
lib.set_fn_2("+", |_: (), y: ImmutableString| Ok(y));
|
lib.set_fn_2("+", |_: (), y: ImmutableString| Ok(y));
|
||||||
|
|
||||||
#[cfg(not(feature = "only_i32"))]
|
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
|
||||||
#[cfg(not(feature = "only_i64"))]
|
|
||||||
{
|
|
||||||
reg_op!(lib, "+", append, i8, u8, i16, u16, i32, i64, u32, u64);
|
reg_op!(lib, "+", append, i8, u8, i16, u16, i32, i64, u32, u64);
|
||||||
reg_op!(lib, "+", prepend, i8, u8, i16, u16, i32, i64, u32, u64);
|
reg_op!(lib, "+", prepend, i8, u8, i16, u16, i32, i64, u32, u64);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
if cfg!(not(target_arch = "wasm32")) {
|
||||||
{
|
|
||||||
reg_op!(lib, "+", append, i128, u128);
|
reg_op!(lib, "+", append, i128, u128);
|
||||||
reg_op!(lib, "+", prepend, i128, u128);
|
reg_op!(lib, "+", prepend, i128, u128);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
use crate::any::{Dynamic, Union};
|
use crate::any::{Dynamic, Union};
|
||||||
use crate::calc_fn_hash;
|
use crate::calc_fn_hash;
|
||||||
use crate::engine::{Engine, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT};
|
use crate::engine::{
|
||||||
|
Engine, KEYWORD_FN_PTR_CURRY, KEYWORD_THIS, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT,
|
||||||
|
};
|
||||||
use crate::error::{LexError, ParseError, ParseErrorType};
|
use crate::error::{LexError, ParseError, ParseErrorType};
|
||||||
use crate::fn_native::Shared;
|
use crate::fn_native::Shared;
|
||||||
use crate::module::{Module, ModuleRef};
|
use crate::module::{Module, ModuleRef};
|
||||||
@ -15,9 +17,6 @@ use crate::utils::{StaticVec, StraightHasherBuilder};
|
|||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
use crate::engine::FN_ANONYMOUS;
|
use crate::engine::FN_ANONYMOUS;
|
||||||
|
|
||||||
#[cfg(not(feature = "no_capture"))]
|
|
||||||
use crate::engine::KEYWORD_FN_PTR_CURRY;
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
use crate::engine::{make_getter, make_setter};
|
use crate::engine::{make_getter, make_setter};
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ use crate::stdlib::{
|
|||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
char,
|
char,
|
||||||
collections::{HashMap, HashSet},
|
collections::HashMap,
|
||||||
fmt, format,
|
fmt, format,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
iter::empty,
|
iter::empty,
|
||||||
@ -40,6 +39,9 @@ use crate::stdlib::{
|
|||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
use crate::stdlib::collections::hash_map::DefaultHasher;
|
use crate::stdlib::collections::hash_map::DefaultHasher;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
use crate::stdlib::collections::HashSet;
|
||||||
|
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
use ahash::AHasher;
|
use ahash::AHasher;
|
||||||
@ -2211,13 +2213,15 @@ fn parse_binary_op(
|
|||||||
|
|
||||||
let (op_token, pos) = input.next().unwrap();
|
let (op_token, pos) = input.next().unwrap();
|
||||||
|
|
||||||
#[cfg(any(not(feature = "no_object"), not(feature = "no_capture")))]
|
if cfg!(not(feature = "no_object")) && op_token == Token::Period {
|
||||||
if op_token == Token::Period {
|
|
||||||
if let (Token::Identifier(_), _) = input.peek().unwrap() {
|
if let (Token::Identifier(_), _) = input.peek().unwrap() {
|
||||||
// prevents capturing of the object properties as vars: xxx.<var>
|
// prevents capturing of the object properties as vars: xxx.<var>
|
||||||
|
#[cfg(not(feature = "no_capture"))]
|
||||||
|
{
|
||||||
state.capture = false;
|
state.capture = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let rhs = parse_unary(input, state, lib, settings)?;
|
let rhs = parse_unary(input, state, lib, settings)?;
|
||||||
|
|
||||||
@ -3112,7 +3116,6 @@ fn parse_fn(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a curried expression from a list of external variables
|
/// Creates a curried expression from a list of external variables
|
||||||
#[cfg(not(feature = "no_capture"))]
|
|
||||||
fn make_curry_from_externals(
|
fn make_curry_from_externals(
|
||||||
fn_expr: Expr,
|
fn_expr: Expr,
|
||||||
externals: StaticVec<(String, Position)>,
|
externals: StaticVec<(String, Position)>,
|
||||||
@ -3209,26 +3212,31 @@ fn parse_anon_fn(
|
|||||||
let body = parse_stmt(input, state, lib, settings.level_up())
|
let body = parse_stmt(input, state, lib, settings.level_up())
|
||||||
.map(|stmt| stmt.unwrap_or_else(|| Stmt::Noop(pos)))?;
|
.map(|stmt| stmt.unwrap_or_else(|| Stmt::Noop(pos)))?;
|
||||||
|
|
||||||
#[cfg(feature = "no_capture")]
|
|
||||||
let params: StaticVec<_> = params.into_iter().map(|(v, _)| v).collect();
|
|
||||||
|
|
||||||
// External variables may need to be processed in a consistent order,
|
// External variables may need to be processed in a consistent order,
|
||||||
// so extract them into a list.
|
// so extract them into a list.
|
||||||
|
let externals: StaticVec<_> = {
|
||||||
#[cfg(not(feature = "no_capture"))]
|
#[cfg(not(feature = "no_capture"))]
|
||||||
let externals: StaticVec<_> = state
|
{
|
||||||
|
state
|
||||||
.externals
|
.externals
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, &v)| (k.clone(), v))
|
.map(|(k, &v)| (k.clone(), v))
|
||||||
.collect();
|
.collect()
|
||||||
|
}
|
||||||
|
#[cfg(feature = "no_capture")]
|
||||||
|
Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
// Add parameters that are auto-curried
|
let params: StaticVec<_> = if cfg!(not(feature = "no_capture")) {
|
||||||
#[cfg(not(feature = "no_capture"))]
|
externals
|
||||||
let params: StaticVec<_> = externals
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(k, _)| k)
|
.map(|(k, _)| k)
|
||||||
.cloned()
|
.cloned()
|
||||||
.chain(params.into_iter().map(|(v, _)| v))
|
.chain(params.into_iter().map(|(v, _)| v))
|
||||||
.collect();
|
.collect()
|
||||||
|
} else {
|
||||||
|
params.into_iter().map(|(v, _)| v).collect()
|
||||||
|
};
|
||||||
|
|
||||||
// Calculate hash
|
// Calculate hash
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
@ -3257,8 +3265,11 @@ fn parse_anon_fn(
|
|||||||
|
|
||||||
let expr = Expr::FnPointer(Box::new((fn_name, settings.pos)));
|
let expr = Expr::FnPointer(Box::new((fn_name, settings.pos)));
|
||||||
|
|
||||||
#[cfg(not(feature = "no_capture"))]
|
let expr = if cfg!(not(feature = "no_capture")) {
|
||||||
let expr = make_curry_from_externals(expr, externals, settings.pos);
|
make_curry_from_externals(expr, externals, settings.pos)
|
||||||
|
} else {
|
||||||
|
expr
|
||||||
|
};
|
||||||
|
|
||||||
Ok((expr, script))
|
Ok((expr, script))
|
||||||
}
|
}
|
||||||
|
55
src/scope.rs
55
src/scope.rs
@ -158,6 +158,32 @@ impl<'a> Scope<'a> {
|
|||||||
self.push_dynamic_value(name, EntryType::Normal, Dynamic::from(value), false)
|
self.push_dynamic_value(name, EntryType::Normal, Dynamic::from(value), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add (push) a new shared entry to the Scope.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use rhai::Scope;
|
||||||
|
///
|
||||||
|
/// let mut my_scope = Scope::new();
|
||||||
|
///
|
||||||
|
/// my_scope.push_shared("x", 42_i64);
|
||||||
|
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
|
||||||
|
/// ```
|
||||||
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
pub fn push_shared<K: Into<Cow<'a, str>>, T: Variant + Clone>(
|
||||||
|
&mut self,
|
||||||
|
name: K,
|
||||||
|
value: T,
|
||||||
|
) -> &mut Self {
|
||||||
|
self.push_dynamic_value(
|
||||||
|
name,
|
||||||
|
EntryType::Normal,
|
||||||
|
Dynamic::from(value).into_shared(),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Add (push) a new `Dynamic` entry to the Scope.
|
/// Add (push) a new `Dynamic` entry to the Scope.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -200,6 +226,34 @@ impl<'a> Scope<'a> {
|
|||||||
self.push_dynamic_value(name, EntryType::Constant, Dynamic::from(value), true)
|
self.push_dynamic_value(name, EntryType::Constant, Dynamic::from(value), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add (push) a new shared constant to the Scope.
|
||||||
|
///
|
||||||
|
/// Shared constants are immutable and cannot be assigned to, but their shared values can change.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use rhai::Scope;
|
||||||
|
///
|
||||||
|
/// let mut my_scope = Scope::new();
|
||||||
|
///
|
||||||
|
/// my_scope.push_constant_shared("x", 42_i64);
|
||||||
|
/// assert_eq!(my_scope.get_value::<i64>("x").unwrap(), 42);
|
||||||
|
/// ```
|
||||||
|
#[cfg(not(feature = "no_shared"))]
|
||||||
|
pub fn push_constant_shared<K: Into<Cow<'a, str>>, T: Variant + Clone>(
|
||||||
|
&mut self,
|
||||||
|
name: K,
|
||||||
|
value: T,
|
||||||
|
) -> &mut Self {
|
||||||
|
self.push_dynamic_value(
|
||||||
|
name,
|
||||||
|
EntryType::Constant,
|
||||||
|
Dynamic::from(value).into_shared(),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Add (push) a new constant with a `Dynamic` value to the Scope.
|
/// Add (push) a new constant with a `Dynamic` value to the Scope.
|
||||||
///
|
///
|
||||||
/// Constants are immutable and cannot be assigned to. Their values never change.
|
/// Constants are immutable and cannot be assigned to. Their values never change.
|
||||||
@ -393,7 +447,6 @@ impl<'a> Scope<'a> {
|
|||||||
|
|
||||||
/// Clone the Scope, keeping only the last instances of each variable name.
|
/// Clone the Scope, keeping only the last instances of each variable name.
|
||||||
/// Shadowed variables are omitted in the copy.
|
/// Shadowed variables are omitted in the copy.
|
||||||
#[cfg(not(feature = "no_capture"))]
|
|
||||||
pub(crate) fn flatten_clone(&self) -> Self {
|
pub(crate) fn flatten_clone(&self) -> Self {
|
||||||
let mut entries: HashMap<&str, Entry> = Default::default();
|
let mut entries: HashMap<&str, Entry> = Default::default();
|
||||||
|
|
||||||
|
@ -507,8 +507,8 @@ impl Token {
|
|||||||
| "yield" => Reserved(syntax.into()),
|
| "yield" => Reserved(syntax.into()),
|
||||||
|
|
||||||
KEYWORD_PRINT | KEYWORD_DEBUG | KEYWORD_TYPE_OF | KEYWORD_EVAL | KEYWORD_FN_PTR
|
KEYWORD_PRINT | KEYWORD_DEBUG | KEYWORD_TYPE_OF | KEYWORD_EVAL | KEYWORD_FN_PTR
|
||||||
| KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_SHARED | KEYWORD_TAKE
|
| KEYWORD_FN_PTR_CALL | KEYWORD_FN_PTR_CURRY | KEYWORD_IS_SHARED | KEYWORD_SHARED
|
||||||
| KEYWORD_THIS => Reserved(syntax.into()),
|
| KEYWORD_TAKE | KEYWORD_THIS => Reserved(syntax.into()),
|
||||||
|
|
||||||
_ => return None,
|
_ => return None,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user