rhai/eval/stmt.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
//! Module defining functions for evaluating a statement.
use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::ast::{
ASTFlags, BinaryExpr, Expr, FlowControl, OpAssignment, Stmt, SwitchCasesCollection,
};
use crate::func::{get_builtin_op_assignment_fn, get_hasher};
use crate::tokenizer::Token;
use crate::types::dynamic::{AccessMode, Union};
use crate::{Dynamic, Engine, RhaiResult, RhaiResultOf, Scope, VarDefInfo, ERR, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
convert::TryInto,
hash::{Hash, Hasher},
};
impl Engine {
/// If the value is a string, intern it.
#[inline(always)]
fn intern_string(&self, value: Dynamic) -> Dynamic {
match value.0 {
Union::Str(s, ..) => self.get_interned_string(s).into(),
_ => value,
}
}
/// Evaluate a statements block.
pub(crate) fn eval_stmt_block(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &mut Scope,
mut this_ptr: Option<&mut Dynamic>,
statements: &[Stmt],
restore_orig_state: bool,
) -> RhaiResult {
if statements.is_empty() {
return Ok(Dynamic::UNIT);
}
// Restore scope at end of block if necessary
defer! { scope if restore_orig_state => rewind; let orig_scope_len = scope.len(); }
// Restore global state at end of block if necessary
let orig_always_search_scope = global.always_search_scope;
#[cfg(not(feature = "no_module"))]
let orig_imports_len = global.num_imports();
if restore_orig_state {
global.scope_level += 1;
}
defer! { global if restore_orig_state => move |g| {
g.scope_level -= 1;
#[cfg(not(feature = "no_module"))]
g.truncate_imports(orig_imports_len);
// The impact of new local variables goes away at the end of a block
// because any new variables introduced will go out of scope
g.always_search_scope = orig_always_search_scope;
}}
// Pop new function resolution caches at end of block
defer! {
caches => rewind_fn_resolution_caches;
let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len();
}
// Run the statements
statements.iter().try_fold(Dynamic::UNIT, |_, stmt| {
let this_ptr = this_ptr.as_deref_mut();
#[cfg(not(feature = "no_module"))]
let orig_imports_len = global.num_imports();
let result =
self.eval_stmt(global, caches, scope, this_ptr, stmt, restore_orig_state)?;
#[cfg(not(feature = "no_module"))]
if matches!(stmt, Stmt::Import(..)) {
// Get the extra modules - see if any functions are marked global.
// Without global functions, the extra modules never affect function resolution.
if global
.scan_imports_raw()
.skip(orig_imports_len)
.any(|(.., m)| m.contains_indexed_global_functions())
{
// Different scenarios where the cache must be cleared - notice that this is
// expensive as all function resolutions must start again
if caches.fn_resolution_caches_len() > orig_fn_resolution_caches_len {
// When new module is imported with global functions and there is already
// a new cache, just clear it
caches.fn_resolution_cache_mut().clear();
} else if restore_orig_state {
// When new module is imported with global functions, push a new cache
caches.push_fn_resolution_cache();
} else {
// When the block is to be evaluated in-place, just clear the current cache
caches.fn_resolution_cache_mut().clear();
}
}
}
Ok(result)
})
}
/// Evaluate an op-assignment statement.
pub(crate) fn eval_op_assignment(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
op_info: &OpAssignment,
root: &Expr,
target: &mut Target,
mut new_val: Dynamic,
) -> RhaiResultOf<()> {
// Assignment to constant variable?
if target.as_ref().is_read_only() {
let name = root.get_variable_name(false).unwrap_or_default();
let pos = root.start_position();
return Err(ERR::ErrorAssignmentToConstant(name.to_string(), pos).into());
}
let pos = op_info.position();
if let Some((hash_x, hash, op_x, op_x_str, op, op_str)) = op_info.get_op_assignment_info() {
let mut lock_guard = target.as_mut().write_lock::<Dynamic>().unwrap();
let mut done = false;
// Short-circuit built-in op-assignments if under Fast Operators mode
if self.fast_operators() {
#[allow(clippy::wildcard_imports)]
use Token::*;
done = true;
// For extremely simple primary data operations, do it directly
// to avoid the overhead of calling a function.
match (&mut lock_guard.0, &mut new_val.0) {
(Union::Bool(b1, ..), Union::Bool(b2, ..)) => match op_x {
AndAssign => *b1 = *b1 && *b2,
OrAssign => *b1 = *b1 || *b2,
XOrAssign => *b1 ^= *b2,
_ => done = false,
},
(Union::Int(n1, ..), Union::Int(n2, ..)) => {
#[cfg(not(feature = "unchecked"))]
#[allow(clippy::wildcard_imports)]
use crate::packages::arithmetic::arith_basic::INT::functions::*;
#[cfg(not(feature = "unchecked"))]
match op_x {
PlusAssign => {
*n1 = add(*n1, *n2).map_err(|err| err.fill_position(pos))?
}
MinusAssign => {
*n1 = subtract(*n1, *n2).map_err(|err| err.fill_position(pos))?
}
MultiplyAssign => {
*n1 = multiply(*n1, *n2).map_err(|err| err.fill_position(pos))?
}
DivideAssign => {
*n1 = divide(*n1, *n2).map_err(|err| err.fill_position(pos))?
}
ModuloAssign => {
*n1 = modulo(*n1, *n2).map_err(|err| err.fill_position(pos))?
}
_ => done = false,
}
#[cfg(feature = "unchecked")]
match op_x {
PlusAssign => *n1 += *n2,
MinusAssign => *n1 -= *n2,
MultiplyAssign => *n1 *= *n2,
DivideAssign => *n1 /= *n2,
ModuloAssign => *n1 %= *n2,
_ => done = false,
}
}
#[cfg(not(feature = "no_float"))]
(Union::Float(f1, ..), Union::Float(f2, ..)) => match op_x {
PlusAssign => **f1 += **f2,
MinusAssign => **f1 -= **f2,
MultiplyAssign => **f1 *= **f2,
DivideAssign => **f1 /= **f2,
ModuloAssign => **f1 %= **f2,
_ => done = false,
},
#[cfg(not(feature = "no_float"))]
(Union::Float(f1, ..), Union::Int(n2, ..)) => match op_x {
PlusAssign => **f1 += *n2 as crate::FLOAT,
MinusAssign => **f1 -= *n2 as crate::FLOAT,
MultiplyAssign => **f1 *= *n2 as crate::FLOAT,
DivideAssign => **f1 /= *n2 as crate::FLOAT,
ModuloAssign => **f1 %= *n2 as crate::FLOAT,
_ => done = false,
},
_ => done = false,
}
if !done {
if let Some((func, need_context)) =
get_builtin_op_assignment_fn(op_x, &lock_guard, &new_val)
{
// We may not need to bump the level because built-in's do not need it.
//defer! { let orig_level = global.level; global.level += 1 }
let args = &mut [&mut *lock_guard, &mut new_val];
let context = need_context
.then(|| (self, op_x_str, global.source(), &*global, pos).into());
let _ = func(context, args).map_err(|err| err.fill_position(pos))?;
done = true;
}
}
}
if !done {
let opx = Some(op_x);
let args = &mut [&mut *lock_guard, &mut new_val];
match self.exec_native_fn_call(
global, caches, op_x_str, opx, hash_x, args, true, false, pos,
) {
Ok(_) => (),
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_x_str)) =>
{
// Expand to `var = var op rhs`
let op = Some(op);
*args[0] = self
.exec_native_fn_call(
global, caches, op_str, op, hash, args, true, false, pos,
)?
.0;
}
Err(err) => return Err(err),
}
self.check_data_size(&*args[0], root.position())?;
}
} else {
// Normal assignment
match target {
// Lock it again just in case it is shared
Target::RefMut(_) | Target::TempValue(_) => {
*target.as_mut().write_lock::<Dynamic>().unwrap() = new_val
}
#[allow(unreachable_patterns)]
_ => *target.as_mut() = new_val,
}
}
target.propagate_changed_value(pos)
}
/// Evaluate a statement.
pub(crate) fn eval_stmt(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &mut Scope,
mut this_ptr: Option<&mut Dynamic>,
stmt: &Stmt,
rewind_scope: bool,
) -> RhaiResult {
self.track_operation(global, stmt.position())?;
#[cfg(feature = "debugging")]
let reset = self.dbg_reset(global, caches, scope, this_ptr.as_deref_mut(), stmt)?;
#[cfg(feature = "debugging")]
defer! { global if Some(reset) => move |g| g.debugger_mut().reset_status(reset) }
match stmt {
// No-op
Stmt::Noop(..) => Ok(Dynamic::UNIT),
// Expression as statement
Stmt::Expr(expr) => self
.eval_expr(global, caches, scope, this_ptr, expr)
.map(Dynamic::flatten),
// Block scope
Stmt::Block(stmts, ..) => {
if stmts.is_empty() {
Ok(Dynamic::UNIT)
} else {
self.eval_stmt_block(global, caches, scope, this_ptr, stmts.statements(), true)
}
}
// Function call
Stmt::FnCall(x, pos) => {
self.eval_fn_call_expr(global, caches, scope, this_ptr, x, *pos)
}
// Assignment
Stmt::Assignment(x, ..) => {
let (op_info, BinaryExpr { lhs, rhs }) = &**x;
if let Expr::ThisPtr(..) = lhs {
if this_ptr.is_none() {
return Err(ERR::ErrorUnboundThis(lhs.position()).into());
}
#[cfg(not(feature = "no_function"))]
{
let rhs_val = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), rhs)?
.flatten();
self.track_operation(global, lhs.position())?;
let target = &mut this_ptr.unwrap().try_into()?;
self.eval_op_assignment(global, caches, op_info, lhs, target, rhs_val)?;
}
#[cfg(feature = "no_function")]
unreachable!();
} else if let Expr::Variable(x, ..) = lhs {
let rhs_val = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), rhs)?
.flatten();
self.track_operation(global, lhs.position())?;
let mut target = self.search_namespace(global, caches, scope, this_ptr, lhs)?;
let is_temp_result = !target.is_ref();
#[cfg(not(feature = "no_closure"))]
// Also handle case where target is a `Dynamic` shared value
// (returned by a variable resolver, for example)
let is_temp_result = is_temp_result && !target.is_shared();
// Cannot assign to temp result from expression
if is_temp_result {
return Err(ERR::ErrorAssignmentToConstant(
x.1.to_string(),
lhs.position(),
)
.into());
}
self.eval_op_assignment(global, caches, op_info, lhs, &mut target, rhs_val)?;
} else {
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
{
let rhs_val = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), rhs)?
.flatten();
let _new_val = Some((self.intern_string(rhs_val), op_info));
// Must be either `var[index] op= val` or `var.prop op= val`.
// The return value of any op-assignment (should be `()`) is thrown away and not used.
let _ = match lhs {
// this op= rhs (handled above)
Expr::ThisPtr(..) => {
unreachable!("Expr::ThisPtr case is already handled")
}
// name op= rhs (handled above)
Expr::Variable(..) => {
unreachable!("Expr::Variable case is already handled")
}
// idx_lhs[idx_expr] op= rhs
#[cfg(not(feature = "no_index"))]
Expr::Index(..) => self.eval_dot_index_chain(
global, caches, scope, this_ptr, lhs, _new_val,
),
// dot_lhs.dot_rhs op= rhs
#[cfg(not(feature = "no_object"))]
Expr::Dot(..) => self.eval_dot_index_chain(
global, caches, scope, this_ptr, lhs, _new_val,
),
_ => unreachable!("cannot assign to expression: {:?}", lhs),
}?;
}
}
Ok(Dynamic::UNIT)
}
// Variable definition
Stmt::Var(x, options, pos) => {
if !self.allow_shadowing() && scope.contains(x.0.as_str()) {
return Err(ERR::ErrorVariableExists(x.0.as_str().to_string(), *pos).into());
}
// Let/const statement
let (var_name, expr, index) = &**x;
let access = if options.intersects(ASTFlags::CONSTANT) {
AccessMode::ReadOnly
} else {
AccessMode::ReadWrite
};
let export = options.intersects(ASTFlags::EXPORTED);
// Check variable definition filter
if let Some(ref filter) = self.def_var_filter {
let will_shadow = scope.contains(var_name.as_str());
let is_const = access == AccessMode::ReadOnly;
let info = VarDefInfo::new(
var_name.as_str(),
is_const,
global.scope_level,
will_shadow,
);
let orig_scope_len = scope.len();
let context =
EvalContext::new(self, global, caches, scope, this_ptr.as_deref_mut());
let filter_result = filter(true, info, context);
if orig_scope_len != scope.len() {
// The scope is changed, always search from now on
global.always_search_scope = true;
}
if !filter_result? {
return Err(ERR::ErrorForbiddenVariable(
var_name.as_str().to_string(),
*pos,
)
.into());
}
}
// Guard against too many variables
#[cfg(not(feature = "unchecked"))]
if index.is_none() && scope.len() >= self.max_variables() {
return Err(ERR::ErrorTooManyVariables(*pos).into());
}
// Evaluate initial value
let value = self
.eval_expr(global, caches, scope, this_ptr, expr)?
.flatten();
let mut value = self.intern_string(value);
let _alias = if !rewind_scope {
// Put global constants into global module
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))]
if global.scope_level == 0
&& access == AccessMode::ReadOnly
&& global.lib.iter().any(|m| !m.is_empty())
{
crate::func::locked_write(global.constants.get_or_insert_with(|| {
crate::Shared::new(
crate::Locked::new(std::collections::BTreeMap::new()),
)
}))
.unwrap()
.insert(var_name.name.clone(), value.clone());
}
export.then_some(var_name)
} else if !export {
None
} else {
unreachable!("exported variable not on global level");
};
match index {
Some(index) => {
value.set_access_mode(access);
*scope.get_mut_by_index(scope.len() - index.get()) = value;
}
_ => {
scope.push_entry(var_name.name.clone(), access, value);
}
}
#[cfg(not(feature = "no_module"))]
if let Some(alias) = _alias {
scope.add_alias_by_index(scope.len() - 1, alias.as_str().into());
}
Ok(Dynamic::UNIT)
}
// If statement
Stmt::If(x, ..) => {
let FlowControl { expr, body, branch } = &**x;
let guard_val = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
if guard_val && !body.is_empty() {
self.eval_stmt_block(global, caches, scope, this_ptr, body.statements(), true)
} else if !guard_val && !branch.is_empty() {
self.eval_stmt_block(global, caches, scope, this_ptr, branch.statements(), true)
} else {
Ok(Dynamic::UNIT)
}
}
// Switch statement
Stmt::Switch(x, ..) => {
let (
expr,
SwitchCasesCollection {
expressions,
cases,
def_case,
ranges,
},
) = &**x;
let mut result = None;
let value = self.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?;
if value.is_hashable() {
let hasher = &mut get_hasher();
value.hash(hasher);
let hash = hasher.finish();
// First check hashes
if let Some(case_blocks_list) = cases.get(&hash) {
debug_assert!(!case_blocks_list.is_empty());
for &index in case_blocks_list {
let block = &expressions[index];
let cond_result = match block.lhs {
Expr::BoolConstant(b, ..) => b,
ref c => self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), c)?
.as_bool()
.map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, c.position())
})?,
};
if cond_result {
result = Some(&block.rhs);
break;
}
}
} else if !ranges.is_empty() {
// Then check integer ranges
for r in ranges.iter().filter(|r| r.contains(&value)) {
let BinaryExpr { lhs, rhs } = &expressions[r.index()];
let cond_result = match lhs {
Expr::BoolConstant(b, ..) => *b,
c => self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), c)?
.as_bool()
.map_err(|typ| {
self.make_type_mismatch_err::<bool>(typ, c.position())
})?,
};
if cond_result {
result = Some(rhs);
break;
}
}
}
}
result
.or_else(|| def_case.as_ref().map(|&index| &expressions[index].rhs))
.map_or(Ok(Dynamic::UNIT), |expr| {
self.eval_expr(global, caches, scope, this_ptr, expr)
})
}
// Loop
Stmt::While(x, ..)
if matches!(x.expr, Expr::Unit(..) | Expr::BoolConstant(true, ..)) =>
{
let FlowControl { body, .. } = &**x;
if body.is_empty() {
loop {
self.track_operation(global, body.position())?;
}
}
loop {
let this_ptr = this_ptr.as_deref_mut();
let statements = body.statements();
match self.eval_stmt_block(global, caches, scope, this_ptr, statements, true) {
Ok(..) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => (),
ERR::LoopBreak(true, value, ..) => break Ok(value),
_ => break Err(err),
},
}
}
}
// While loop
Stmt::While(x, ..) => {
let FlowControl { expr, body, .. } = &**x;
loop {
let condition = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
if !condition {
break Ok(Dynamic::UNIT);
}
if body.is_empty() {
continue;
}
let this_ptr = this_ptr.as_deref_mut();
let statements = body.statements();
match self.eval_stmt_block(global, caches, scope, this_ptr, statements, true) {
Ok(..) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => (),
ERR::LoopBreak(true, value, ..) => break Ok(value),
_ => break Err(err),
},
}
}
}
// Do loop
Stmt::Do(x, options, ..) => {
let FlowControl { expr, body, .. } = &**x;
let is_while = !options.intersects(ASTFlags::NEGATED);
loop {
if !body.is_empty() {
let this_ptr = this_ptr.as_deref_mut();
let statements = body.statements();
match self
.eval_stmt_block(global, caches, scope, this_ptr, statements, true)
{
Ok(..) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => continue,
ERR::LoopBreak(true, value, ..) => break Ok(value),
_ => break Err(err),
},
}
}
let condition = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.as_bool()
.map_err(|typ| self.make_type_mismatch_err::<bool>(typ, expr.position()))?;
if condition ^ is_while {
break Ok(Dynamic::UNIT);
}
}
}
// For loop
Stmt::For(x, ..) => {
let (var_name, counter, FlowControl { expr, body, .. }) = &**x;
// Guard against too many variables
#[cfg(not(feature = "unchecked"))]
if scope.len() >= self.max_variables() - usize::from(counter.is_some()) {
return Err(ERR::ErrorTooManyVariables(var_name.pos).into());
}
let iter_obj = self
.eval_expr(global, caches, scope, this_ptr.as_deref_mut(), expr)?
.flatten();
let iter_type = iter_obj.type_id();
// lib should only contain scripts, so technically they cannot have iterators
// Search order:
// 1) Global namespace - functions registered via Engine::register_XXX
// 2) Global modules - packages
// 3) Imported modules - functions marked with global namespace
// 4) Global sub-modules - functions marked with global namespace
let iter_func = self
.global_modules
.iter()
.find_map(|m| m.get_iter(iter_type));
#[cfg(not(feature = "no_module"))]
let iter_func = iter_func
.or_else(|| global.get_iter(iter_type))
.or_else(|| {
self.global_sub_modules
.values()
.find_map(|m| m.get_qualified_iter(iter_type))
});
let iter_func = iter_func.ok_or_else(|| ERR::ErrorFor(expr.start_position()))?;
// Restore scope at end of statement
defer! { scope => rewind; let orig_scope_len = scope.len(); }
// Add the loop variables
let counter_index = counter.as_ref().map(|counter| {
scope.push(counter.name.clone(), 0 as INT);
scope.len() - 1
});
scope.push(var_name.name.clone(), ());
let index = scope.len() - 1;
let mut result = Dynamic::UNIT;
if body.is_empty() {
for _ in iter_func(iter_obj) {
self.track_operation(global, body.position())?;
}
} else {
for (i, iter_value) in iter_func(iter_obj).enumerate() {
// Increment counter
if let Some(counter_index) = counter_index {
// As the variable increments from 0, this should always work
// since any overflow will first be caught below.
let index_value = i as INT;
#[cfg(not(feature = "unchecked"))]
#[allow(clippy::absurd_extreme_comparisons)]
if index_value > crate::MAX_USIZE_INT {
return Err(ERR::ErrorArithmetic(
format!("for-loop counter overflow: {i}"),
counter.as_ref().unwrap().pos,
)
.into());
}
*scope.get_mut_by_index(counter_index).write_lock().unwrap() =
Dynamic::from_int(index_value);
}
// Set loop value
let value = iter_value
.map_err(|err| err.fill_position(expr.position()))?
.flatten();
*scope.get_mut_by_index(index).write_lock().unwrap() = value;
// Run block
let this_ptr = this_ptr.as_deref_mut();
let statements = body.statements();
match self
.eval_stmt_block(global, caches, scope, this_ptr, statements, true)
{
Ok(_) => (),
Err(err) => match *err {
ERR::LoopBreak(false, ..) => (),
ERR::LoopBreak(true, value, ..) => {
result = value;
break;
}
_ => return Err(err),
},
}
}
}
Ok(result)
}
// Continue/Break statement
Stmt::BreakLoop(expr, options, pos) => {
let is_break = options.intersects(ASTFlags::BREAK);
let value = match expr {
Some(ref expr) => self.eval_expr(global, caches, scope, this_ptr, expr)?,
None => Dynamic::UNIT,
};
Err(ERR::LoopBreak(is_break, value, *pos).into())
}
// Try/Catch statement
Stmt::TryCatch(x, ..) => {
let FlowControl {
body,
expr: catch_var,
branch,
} = &**x;
match self.eval_stmt_block(
global,
caches,
scope,
this_ptr.as_deref_mut(),
body.statements(),
true,
) {
r @ Ok(_) => r,
Err(err) if err.is_pseudo_error() => Err(err),
Err(err) if !err.is_catchable() => Err(err),
Err(mut err) => {
let err_value = match err.unwrap_inner() {
// No error variable
_ if catch_var.is_unit() => Dynamic::UNIT,
ERR::ErrorRuntime(x, ..) => x.clone(),
#[cfg(feature = "no_object")]
_ => {
let _ = err.take_position();
err.to_string().into()
}
#[cfg(not(feature = "no_object"))]
_ => {
let mut err_map = crate::Map::new();
let err_pos = err.take_position();
err_map.insert("message".into(), err.to_string().into());
if let Some(ref source) = global.source {
err_map.insert("source".into(), source.into());
}
if !err_pos.is_none() {
err_map.insert(
"line".into(),
(err_pos.line().unwrap() as INT).into(),
);
err_map.insert(
"position".into(),
(err_pos.position().unwrap_or(0) as INT).into(),
);
}
err.dump_fields(&mut err_map);
err_map.into()
}
};
// Restore scope at end of block
defer! { scope if !catch_var.is_unit() => rewind; let orig_scope_len = scope.len(); }
if let Expr::Variable(x, ..) = catch_var {
// Guard against too many variables
#[cfg(not(feature = "unchecked"))]
if scope.len() >= self.max_variables() {
return Err(ERR::ErrorTooManyVariables(catch_var.position()).into());
}
scope.push(x.1.clone(), err_value);
}
let this_ptr = this_ptr.as_deref_mut();
let statements = branch.statements();
self.eval_stmt_block(global, caches, scope, this_ptr, statements, true)
.map(|_| Dynamic::UNIT)
.map_err(|result_err| match *result_err {
// Re-throw exception
ERR::ErrorRuntime(v, pos) if v.is_unit() => {
err.set_position(pos);
err
}
_ => result_err,
})
}
}
}
// Throw value
Stmt::Return(Some(expr), options, pos) if options.intersects(ASTFlags::BREAK) => self
.eval_expr(global, caches, scope, this_ptr, expr)
.and_then(|v| Err(ERR::ErrorRuntime(v.flatten(), *pos).into())),
// Empty throw
Stmt::Return(None, options, pos) if options.intersects(ASTFlags::BREAK) => {
Err(ERR::ErrorRuntime(Dynamic::UNIT, *pos).into())
}
// Return value
Stmt::Return(Some(expr), .., pos) => self
.eval_expr(global, caches, scope, this_ptr, expr)
.and_then(|v| Err(ERR::Return(v.flatten(), *pos).into())),
// Empty return
Stmt::Return(None, .., pos) => Err(ERR::Return(Dynamic::UNIT, *pos).into()),
// Import statement
#[cfg(not(feature = "no_module"))]
Stmt::Import(x, _pos) => {
use crate::ModuleResolver;
let (expr, export) = &**x;
// Guard against too many modules
#[cfg(not(feature = "unchecked"))]
if global.num_modules_loaded >= self.max_modules() {
return Err(ERR::ErrorTooManyModules(*_pos).into());
}
let v = self.eval_expr(global, caches, scope, this_ptr, expr)?;
let path = v.try_cast_result::<crate::ImmutableString>().map_err(|v| {
self.make_type_mismatch_err::<crate::ImmutableString>(
v.type_name(),
expr.position(),
)
})?;
let path_pos = expr.start_position();
let resolver = global.embedded_module_resolver.clone();
let module = resolver
.as_ref()
.and_then(
|r| match r.resolve_raw(self, global, scope, &path, path_pos) {
Err(err) if matches!(*err, ERR::ErrorModuleNotFound(..)) => None,
result => Some(result),
},
)
.or_else(|| {
Some(
self.module_resolver()
.resolve_raw(self, global, scope, &path, path_pos),
)
})
.unwrap_or_else(|| {
Err(ERR::ErrorModuleNotFound(path.to_string(), path_pos).into())
})?;
let (export, must_be_indexed) = if export.is_empty() {
(self.const_empty_string(), false)
} else {
(export.name.clone(), true)
};
if !must_be_indexed || module.is_indexed() {
global.push_import(export, module);
} else {
// Index the module (making a clone copy if necessary) if it is not indexed
let mut m = crate::func::shared_take_or_clone(module);
m.build_index();
global.push_import(export, m);
}
global.num_modules_loaded += 1;
Ok(Dynamic::UNIT)
}
// Export statement
#[cfg(not(feature = "no_module"))]
Stmt::Export(x, ..) => {
use crate::ast::Ident;
let (Ident { name, pos, .. }, Ident { name: alias, .. }) = &**x;
// Mark scope variables as public
scope.search(name).map_or_else(
|| Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into()),
|index| {
let alias = if alias.is_empty() { name } else { alias };
scope.add_alias_by_index(index, alias.clone());
Ok(Dynamic::UNIT)
},
)
}
// Share statement
#[cfg(not(feature = "no_closure"))]
Stmt::Share(x) => {
for (var, index) in &**x {
// Check the variable resolver, if any
if let Some(ref resolve_var) = self.resolve_var {
let orig_scope_len = scope.len();
let context =
EvalContext::new(self, global, caches, scope, this_ptr.as_deref_mut());
let resolved_var = resolve_var(
&var.name,
index.map_or(0, core::num::NonZeroUsize::get),
context,
);
if orig_scope_len != scope.len() {
// The scope is changed, always search from now on
global.always_search_scope = true;
}
match resolved_var {
// If resolved to a variable, skip it (because we cannot make it shared)
Ok(Some(_)) => continue,
Ok(None) => (),
Err(err) => return Err(err.fill_position(var.pos)),
}
}
// Search the scope
let index = index
.map(|n| scope.len() - n.get())
.or_else(|| scope.search(&var.name))
.ok_or_else(|| {
Box::new(ERR::ErrorVariableNotFound(var.name.to_string(), var.pos))
})?;
let val = scope.get_mut_by_index(index);
if !val.is_shared() {
// Replace the variable with a shared value.
*val = val.take().into_shared();
}
}
Ok(Dynamic::UNIT)
}
#[allow(unreachable_patterns)]
_ => unreachable!("statement cannot be evaluated: {:?}", stmt),
}
}
/// Evaluate a list of statements with no `this` pointer.
/// This is commonly used to evaluate a list of statements in an [`AST`][crate::AST] or a script function body.
#[inline(always)]
pub(crate) fn eval_global_statements(
&self,
global: &mut GlobalRuntimeState,
caches: &mut Caches,
scope: &mut Scope,
statements: &[Stmt],
map_exit_to_return_value: bool,
) -> RhaiResult {
self.eval_stmt_block(global, caches, scope, None, statements, false)
.or_else(|err| match *err {
ERR::Return(out, ..) => Ok(out),
ERR::Exit(out, ..) if map_exit_to_return_value => Ok(out),
ERR::LoopBreak(..) => {
unreachable!("no outer loop scope to break out of")
}
_ => Err(err),
})
}
}