rhai 1.10.1

Embedded scripting for Rust
Documentation
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
//! Module defining functions for evaluating a statement.

use super::{Caches, EvalContext, GlobalRuntimeState, Target};
use crate::api::events::VarDefInfo;
use crate::ast::{
    ASTFlags, BinaryExpr, Expr, Ident, OpAssignment, Stmt, SwitchCasesCollection, TryCatchBlock,
};
use crate::func::get_hasher;
use crate::types::dynamic::{AccessMode, Union};
use crate::{
    Dynamic, Engine, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR, INT,
};
use std::hash::{Hash, Hasher};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;

impl Engine {
    /// Evaluate a statements block.
    //
    // # Implementation Notes
    //
    // Do not use the `?` operator within the main body as it makes this function return early,
    // possibly by-passing important cleanup tasks at the end.
    //
    // Errors that are not recoverable, such as system errors or safety errors, can use `?`.
    pub(crate) fn eval_stmt_block(
        &self,
        scope: &mut Scope,
        global: &mut GlobalRuntimeState,
        caches: &mut Caches,
        lib: &[&Module],
        this_ptr: &mut Option<&mut Dynamic>,
        statements: &[Stmt],
        restore_orig_state: bool,
        level: usize,
    ) -> RhaiResult {
        if statements.is_empty() {
            return Ok(Dynamic::UNIT);
        }

        let orig_always_search_scope = global.always_search_scope;
        let orig_scope_len = scope.len();
        #[cfg(not(feature = "no_module"))]
        let orig_imports_len = global.num_imports();
        let orig_fn_resolution_caches_len = caches.fn_resolution_caches_len();

        if restore_orig_state {
            global.scope_level += 1;
        }

        let mut result = Ok(Dynamic::UNIT);

        for stmt in statements {
            #[cfg(not(feature = "no_module"))]
            let imports_len = global.num_imports();

            result = self.eval_stmt(
                scope,
                global,
                caches,
                lib,
                this_ptr,
                stmt,
                restore_orig_state,
                level,
            );

            if result.is_err() {
                break;
            }

            #[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(imports_len)
                    .any(|(.., m)| m.contains_indexed_global_functions())
                {
                    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, clear it - notice that this is expensive as all function
                        // resolutions must start again
                        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();
                    }
                }
            }
        }

        // If imports list is modified, pop the functions lookup cache
        caches.rewind_fn_resolution_caches(orig_fn_resolution_caches_len);

        if restore_orig_state {
            scope.rewind(orig_scope_len);
            global.scope_level -= 1;
            #[cfg(not(feature = "no_module"))]
            global.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
            global.always_search_scope = orig_always_search_scope;
        }

        result
    }

    /// Evaluate an op-assignment statement.
    pub(crate) fn eval_op_assignment(
        &self,
        global: &mut GlobalRuntimeState,
        caches: &mut Caches,
        lib: &[&Module],
        op_info: OpAssignment,
        target: &mut Target,
        root: (&str, Position),
        new_val: Dynamic,
        level: usize,
    ) -> RhaiResultOf<()> {
        if target.is_read_only() {
            // Assignment to constant variable
            return Err(ERR::ErrorAssignmentToConstant(root.0.to_string(), root.1).into());
        }

        let mut new_val = new_val;

        if op_info.is_op_assignment() {
            let OpAssignment {
                hash_op_assign,
                hash_op,
                op_assign,
                op,
                pos: op_pos,
            } = op_info;

            let mut lock_guard = target.write_lock::<Dynamic>().unwrap();

            let hash = hash_op_assign;
            let args = &mut [&mut *lock_guard, &mut new_val];
            let level = level + 1;

            match self.call_native_fn(
                global, caches, lib, op_assign, hash, args, true, true, op_pos, level,
            ) {
                Ok(_) => {
                    #[cfg(not(feature = "unchecked"))]
                    self.check_data_size(args[0], root.1)?;
                }
                Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) =>
                {
                    // Expand to `var = var op rhs`
                    let (value, ..) = self
                        .call_native_fn(
                            global, caches, lib, op, hash_op, args, true, false, op_pos, level,
                        )
                        .map_err(|err| err.fill_position(op_info.pos))?;

                    #[cfg(not(feature = "unchecked"))]
                    self.check_data_size(&value, root.1)?;

                    *args[0] = value.flatten();
                }
                Err(err) => return Err(err),
            }
        } else {
            // Normal assignment
            *target.write_lock::<Dynamic>().unwrap() = new_val;
        }

        /*
        if let Some(mut guard) = target.write_lock::<Dynamic>() {
            if guard.is::<ImmutableString>() {
                let s = std::mem::take(&mut *guard).cast::<ImmutableString>();
                *guard = self.get_interned_string(s).into();
            }
        }
        */

        target.propagate_changed_value(op_info.pos)
    }

    /// Evaluate a statement.
    //
    // # Implementation Notes
    //
    // Do not use the `?` operator within the main body as it makes this function return early,
    // possibly by-passing important cleanup tasks at the end.
    //
    // Errors that are not recoverable, such as system errors or safety errors, can use `?`.
    pub(crate) fn eval_stmt(
        &self,
        scope: &mut Scope,
        global: &mut GlobalRuntimeState,
        caches: &mut Caches,
        lib: &[&Module],
        this_ptr: &mut Option<&mut Dynamic>,
        stmt: &Stmt,
        rewind_scope: bool,
        level: usize,
    ) -> RhaiResult {
        #[cfg(feature = "debugging")]
        let reset_debugger =
            self.run_debugger_with_reset(scope, global, lib, this_ptr, stmt, level)?;

        // Coded this way for better branch prediction.
        // Popular branches are lifted out of the `match` statement into their own branches.

        // Function calls should account for a relatively larger portion of statements.
        if let Stmt::FnCall(x, ..) = stmt {
            #[cfg(not(feature = "unchecked"))]
            self.inc_operations(&mut global.num_operations, stmt.position())?;

            let result =
                self.eval_fn_call_expr(scope, global, caches, lib, this_ptr, x, x.pos, level);

            #[cfg(feature = "debugging")]
            global.debugger.reset_status(reset_debugger);

            return result;
        }

        // Then assignments.
        // We shouldn't do this for too many variants because, soon or later, the added comparisons
        // will cost more than the mis-predicted `match` branch.
        if let Stmt::Assignment(x, ..) = stmt {
            let (op_info, BinaryExpr { lhs, rhs }) = &**x;

            #[cfg(not(feature = "unchecked"))]
            self.inc_operations(&mut global.num_operations, stmt.position())?;

            let result = if let Expr::Variable(x, ..) = lhs {
                let rhs_result = self
                    .eval_expr(scope, global, caches, lib, this_ptr, rhs, level)
                    .map(Dynamic::flatten);

                if let Ok(rhs_val) = rhs_result {
                    let search_result =
                        self.search_namespace(scope, global, lib, this_ptr, lhs, level);

                    if let Ok(search_val) = search_result {
                        let (mut lhs_ptr, pos) = search_val;

                        let var_name = x.3.as_str();

                        #[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 = !lhs_ptr.is_ref() && !lhs_ptr.is_shared();
                        #[cfg(feature = "no_closure")]
                        let is_temp_result = !lhs_ptr.is_ref();

                        // Cannot assign to temp result from expression
                        if is_temp_result {
                            return Err(
                                ERR::ErrorAssignmentToConstant(var_name.to_string(), pos).into()
                            );
                        }

                        #[cfg(not(feature = "unchecked"))]
                        self.inc_operations(&mut global.num_operations, pos)?;

                        let root = (var_name, pos);
                        let lhs_ptr = &mut lhs_ptr;

                        self.eval_op_assignment(
                            global, caches, lib, *op_info, lhs_ptr, root, rhs_val, level,
                        )
                        .map(|_| Dynamic::UNIT)
                    } else {
                        search_result.map(|_| Dynamic::UNIT)
                    }
                } else {
                    rhs_result
                }
            } else {
                let (op_info, BinaryExpr { lhs, rhs }) = &**x;

                let rhs_result = self
                    .eval_expr(scope, global, caches, lib, this_ptr, rhs, level)
                    .map(Dynamic::flatten);

                if let Ok(rhs_val) = rhs_result {
                    let rhs_val = if rhs_val.is::<ImmutableString>() {
                        self.get_interned_string(rhs_val.cast::<ImmutableString>())
                            .into()
                    } else {
                        rhs_val
                    };

                    let _new_val = Some((rhs_val, *op_info));

                    // Must be either `var[index] op= val` or `var.prop op= val`
                    match lhs {
                        // 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(
                                scope, global, caches, lib, this_ptr, lhs, level, _new_val,
                            )
                            .map(|_| Dynamic::UNIT),
                        // dot_lhs.dot_rhs op= rhs
                        #[cfg(not(feature = "no_object"))]
                        Expr::Dot(..) => self
                            .eval_dot_index_chain(
                                scope, global, caches, lib, this_ptr, lhs, level, _new_val,
                            )
                            .map(|_| Dynamic::UNIT),
                        _ => unreachable!("cannot assign to expression: {:?}", lhs),
                    }
                } else {
                    rhs_result
                }
            };

            #[cfg(feature = "debugging")]
            global.debugger.reset_status(reset_debugger);

            return result;
        }

        #[cfg(not(feature = "unchecked"))]
        self.inc_operations(&mut global.num_operations, stmt.position())?;

        let result = match stmt {
            // No-op
            Stmt::Noop(..) => Ok(Dynamic::UNIT),

            // Expression as statement
            Stmt::Expr(expr) => self
                .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                .map(Dynamic::flatten),

            // Block scope
            Stmt::Block(statements, ..) if statements.is_empty() => Ok(Dynamic::UNIT),
            Stmt::Block(statements, ..) => self.eval_stmt_block(
                scope, global, caches, lib, this_ptr, statements, true, level,
            ),

            // If statement
            Stmt::If(x, ..) => {
                let (expr, if_block, else_block) = &**x;

                let guard_val = self
                    .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                    .and_then(|v| {
                        v.as_bool().map_err(|typ| {
                            self.make_type_mismatch_err::<bool>(typ, expr.position())
                        })
                    });

                match guard_val {
                    Ok(true) => {
                        if if_block.is_empty() {
                            Ok(Dynamic::UNIT)
                        } else {
                            self.eval_stmt_block(
                                scope, global, caches, lib, this_ptr, if_block, true, level,
                            )
                        }
                    }
                    Ok(false) => {
                        if else_block.is_empty() {
                            Ok(Dynamic::UNIT)
                        } else {
                            self.eval_stmt_block(
                                scope, global, caches, lib, this_ptr, else_block, true, level,
                            )
                        }
                    }
                    err => err.map(Into::into),
                }
            }

            // Switch statement
            Stmt::Switch(x, ..) => {
                let (
                    expr,
                    SwitchCasesCollection {
                        expressions,
                        cases,
                        def_case,
                        ranges,
                    },
                ) = &**x;

                let value_result =
                    self.eval_expr(scope, global, caches, lib, this_ptr, expr, level);

                if let Ok(value) = value_result {
                    let expr_result = 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) {
                            assert!(!case_blocks_list.is_empty());

                            let mut result = Ok(None);

                            for &index in case_blocks_list {
                                let block = &expressions[index];

                                let cond_result = match block.condition {
                                    Expr::BoolConstant(b, ..) => Ok(b),
                                    ref c => self
                                        .eval_expr(scope, global, caches, lib, this_ptr, c, level)
                                        .and_then(|v| {
                                            v.as_bool().map_err(|typ| {
                                                self.make_type_mismatch_err::<bool>(
                                                    typ,
                                                    c.position(),
                                                )
                                            })
                                        }),
                                };

                                match cond_result {
                                    Ok(true) => {
                                        result = Ok(Some(&block.expr));
                                        break;
                                    }
                                    Ok(false) => (),
                                    _ => {
                                        result = cond_result.map(|_| None);
                                        break;
                                    }
                                }
                            }

                            result
                        } else if value.is::<INT>() && !ranges.is_empty() {
                            // Then check integer ranges
                            let value = value.as_int().expect("`INT`");
                            let mut result = Ok(None);

                            for r in ranges.iter().filter(|r| r.contains(value)) {
                                let block = &expressions[r.index()];

                                let cond_result = match block.condition {
                                    Expr::BoolConstant(b, ..) => Ok(b),
                                    ref c => self
                                        .eval_expr(scope, global, caches, lib, this_ptr, c, level)
                                        .and_then(|v| {
                                            v.as_bool().map_err(|typ| {
                                                self.make_type_mismatch_err::<bool>(
                                                    typ,
                                                    c.position(),
                                                )
                                            })
                                        }),
                                };

                                match cond_result {
                                    Ok(true) => result = Ok(Some(&block.expr)),
                                    Ok(false) => continue,
                                    _ => result = cond_result.map(|_| None),
                                }

                                break;
                            }

                            result
                        } else {
                            // Nothing matches
                            Ok(None)
                        }
                    } else {
                        // Non-hashable
                        Ok(None)
                    };

                    if let Ok(Some(expr)) = expr_result {
                        self.eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                    } else if let Ok(None) = expr_result {
                        // Default match clause
                        def_case.as_ref().map_or(Ok(Dynamic::UNIT), |&index| {
                            let def_expr = &expressions[index].expr;
                            self.eval_expr(scope, global, caches, lib, this_ptr, def_expr, level)
                        })
                    } else {
                        expr_result.map(|_| Dynamic::UNIT)
                    }
                } else {
                    value_result
                }
            }

            // Loop
            Stmt::While(x, ..) if matches!(x.0, Expr::Unit(..)) => loop {
                let (.., body) = &**x;

                if body.is_empty() {
                    #[cfg(not(feature = "unchecked"))]
                    self.inc_operations(&mut global.num_operations, body.position())?;
                } else {
                    match self
                        .eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level)
                    {
                        Ok(_) => (),
                        Err(err) => match *err {
                            ERR::LoopBreak(false, ..) => (),
                            ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
                            _ => break Err(err),
                        },
                    }
                }
            },

            // While loop
            Stmt::While(x, ..) => loop {
                let (expr, body) = &**x;

                let condition = self
                    .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                    .and_then(|v| {
                        v.as_bool().map_err(|typ| {
                            self.make_type_mismatch_err::<bool>(typ, expr.position())
                        })
                    });

                match condition {
                    Ok(false) => break Ok(Dynamic::UNIT),
                    Ok(true) if body.is_empty() => (),
                    Ok(true) => {
                        match self.eval_stmt_block(
                            scope, global, caches, lib, this_ptr, body, true, level,
                        ) {
                            Ok(_) => (),
                            Err(err) => match *err {
                                ERR::LoopBreak(false, ..) => (),
                                ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
                                _ => break Err(err),
                            },
                        }
                    }
                    err => break err.map(|_| Dynamic::UNIT),
                }
            },

            // Do loop
            Stmt::Do(x, options, ..) => loop {
                let (expr, body) = &**x;
                let is_while = !options.contains(ASTFlags::NEGATED);

                if !body.is_empty() {
                    match self
                        .eval_stmt_block(scope, global, caches, lib, this_ptr, body, true, level)
                    {
                        Ok(_) => (),
                        Err(err) => match *err {
                            ERR::LoopBreak(false, ..) => continue,
                            ERR::LoopBreak(true, ..) => break Ok(Dynamic::UNIT),
                            _ => break Err(err),
                        },
                    }
                }

                let condition = self
                    .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                    .and_then(|v| {
                        v.as_bool().map_err(|typ| {
                            self.make_type_mismatch_err::<bool>(typ, expr.position())
                        })
                    });

                match condition {
                    Ok(condition) if condition ^ is_while => break Ok(Dynamic::UNIT),
                    Ok(_) => (),
                    err => break err.map(|_| Dynamic::UNIT),
                }
            },

            // For loop
            Stmt::For(x, ..) => {
                let (var_name, counter, expr, statements) = &**x;

                let iter_result = self
                    .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                    .map(Dynamic::flatten);

                if let Ok(iter_obj) = iter_result {
                    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 func = self
                        .global_modules
                        .iter()
                        .find_map(|m| m.get_iter(iter_type));

                    #[cfg(not(feature = "no_module"))]
                    let func = func.or_else(|| global.get_iter(iter_type)).or_else(|| {
                        self.global_sub_modules
                            .values()
                            .find_map(|m| m.get_qualified_iter(iter_type))
                    });

                    if let Some(func) = func {
                        // Add the loop variables
                        let orig_scope_len = scope.len();
                        let counter_index = if counter.is_empty() {
                            usize::MAX
                        } else {
                            scope.push(counter.name.clone(), 0 as INT);
                            scope.len() - 1
                        };

                        scope.push(var_name.name.clone(), ());
                        let index = scope.len() - 1;

                        let mut loop_result = Ok(Dynamic::UNIT);

                        for (x, iter_value) in func(iter_obj).enumerate() {
                            // Increment counter
                            if counter_index < usize::MAX {
                                // As the variable increments from 0, this should always work
                                // since any overflow will first be caught below.
                                let index_value = x as INT;

                                #[cfg(not(feature = "unchecked"))]
                                if index_value > crate::MAX_USIZE_INT {
                                    loop_result = Err(ERR::ErrorArithmetic(
                                        format!("for-loop counter overflow: {x}"),
                                        counter.pos,
                                    )
                                    .into());
                                    break;
                                }

                                *scope.get_mut_by_index(counter_index).write_lock().unwrap() =
                                    Dynamic::from_int(index_value);
                            }

                            let value = match iter_value {
                                Ok(v) => v.flatten(),
                                Err(err) => {
                                    loop_result = Err(err.fill_position(expr.position()));
                                    break;
                                }
                            };

                            *scope.get_mut_by_index(index).write_lock().unwrap() = value;

                            #[cfg(not(feature = "unchecked"))]
                            if let Err(err) = self
                                .inc_operations(&mut global.num_operations, statements.position())
                            {
                                loop_result = Err(err);
                                break;
                            }

                            if statements.is_empty() {
                                continue;
                            }

                            let result = self.eval_stmt_block(
                                scope, global, caches, lib, this_ptr, statements, true, level,
                            );

                            match result {
                                Ok(_) => (),
                                Err(err) => match *err {
                                    ERR::LoopBreak(false, ..) => (),
                                    ERR::LoopBreak(true, ..) => break,
                                    _ => {
                                        loop_result = Err(err);
                                        break;
                                    }
                                },
                            }
                        }

                        scope.rewind(orig_scope_len);

                        loop_result
                    } else {
                        Err(ERR::ErrorFor(expr.start_position()).into())
                    }
                } else {
                    iter_result
                }
            }

            // Continue/Break statement
            Stmt::BreakLoop(options, pos) => {
                Err(ERR::LoopBreak(options.contains(ASTFlags::BREAK), *pos).into())
            }

            // Try/Catch statement
            Stmt::TryCatch(x, ..) => {
                let TryCatchBlock {
                    try_block,
                    catch_var:
                        Ident {
                            name: catch_var, ..
                        },
                    catch_block,
                } = &**x;

                let result = self
                    .eval_stmt_block(scope, global, caches, lib, this_ptr, try_block, true, level)
                    .map(|_| Dynamic::UNIT);

                match result {
                    Ok(_) => result,
                    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() {
                            ERR::ErrorRuntime(x, ..) => x.clone(),

                            #[cfg(feature = "no_object")]
                            _ => {
                                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 !global.source.is_empty() {
                                    err_map.insert("source".into(), global.source.clone().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()
                            }
                        };

                        let orig_scope_len = scope.len();

                        if !catch_var.is_empty() {
                            scope.push(catch_var.clone(), err_value);
                        }

                        let result = self.eval_stmt_block(
                            scope,
                            global,
                            caches,
                            lib,
                            this_ptr,
                            catch_block,
                            true,
                            level,
                        );

                        scope.rewind(orig_scope_len);

                        match result {
                            Ok(_) => Ok(Dynamic::UNIT),
                            Err(result_err) => match *result_err {
                                // Re-throw exception
                                ERR::ErrorRuntime(Dynamic(Union::Unit(..)), pos) => {
                                    err.set_position(pos);
                                    Err(err)
                                }
                                _ => Err(result_err),
                            },
                        }
                    }
                }
            }

            // Throw value
            Stmt::Return(Some(expr), options, pos) if options.contains(ASTFlags::BREAK) => self
                .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                .and_then(|v| Err(ERR::ErrorRuntime(v.flatten(), *pos).into())),

            // Empty throw
            Stmt::Return(None, options, pos) if options.contains(ASTFlags::BREAK) => {
                Err(ERR::ErrorRuntime(Dynamic::UNIT, *pos).into())
            }

            // Return value
            Stmt::Return(Some(expr), .., pos) => self
                .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                .and_then(|v| Err(ERR::Return(v.flatten(), *pos).into())),

            // Empty return
            Stmt::Return(None, .., pos) => Err(ERR::Return(Dynamic::UNIT, *pos).into()),

            // Let/const statement - shadowing disallowed
            Stmt::Var(x, .., pos) if !self.allow_shadowing() && scope.contains(&x.0) => {
                Err(ERR::ErrorVariableExists(x.0.to_string(), *pos).into())
            }
            // Let/const statement
            Stmt::Var(x, options, pos) => {
                let (var_name, expr, index) = &**x;

                let access = if options.contains(ASTFlags::CONSTANT) {
                    AccessMode::ReadOnly
                } else {
                    AccessMode::ReadWrite
                };
                let export = options.contains(ASTFlags::EXPORTED);

                // Check variable definition filter
                let result = if let Some(ref filter) = self.def_var_filter {
                    let will_shadow = scope.contains(var_name);
                    let nesting_level = global.scope_level;
                    let is_const = access == AccessMode::ReadOnly;
                    let info = VarDefInfo {
                        name: var_name,
                        is_const,
                        nesting_level,
                        will_shadow,
                    };
                    let context = EvalContext::new(self, scope, global, None, lib, this_ptr, level);

                    match filter(true, info, context) {
                        Ok(true) => None,
                        Ok(false) => {
                            Some(Err(
                                ERR::ErrorForbiddenVariable(var_name.to_string(), *pos).into()
                            ))
                        }
                        err @ Err(_) => Some(err),
                    }
                } else {
                    None
                };

                if let Some(result) = result {
                    result.map(|_| Dynamic::UNIT)
                } else {
                    // Evaluate initial value
                    let value_result = self
                        .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                        .map(Dynamic::flatten);

                    if let Ok(mut value) = value_result {
                        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
                                && 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(),
                                        ))
                                    },
                                ))
                                .insert(var_name.name.clone(), value.clone());
                            }

                            if export {
                                Some(var_name)
                            } else {
                                None
                            }
                        } else if export {
                            unreachable!("exported variable not on global level");
                        } else {
                            None
                        };

                        if let Some(index) = index {
                            value.set_access_mode(access);
                            *scope.get_mut_by_index(scope.len() - index.get()) = value;
                        } else {
                            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.name.as_str().into());
                        }

                        Ok(Dynamic::UNIT)
                    } else {
                        value_result
                    }
                }
            }

            // Import statement
            #[cfg(not(feature = "no_module"))]
            Stmt::Import(x, _pos) => {
                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 path_result = self
                    .eval_expr(scope, global, caches, lib, this_ptr, expr, level)
                    .and_then(|v| {
                        let typ = v.type_name();
                        v.try_cast::<crate::ImmutableString>().ok_or_else(|| {
                            self.make_type_mismatch_err::<crate::ImmutableString>(
                                typ,
                                expr.position(),
                            )
                        })
                    });

                if let Ok(path) = path_result {
                    use crate::ModuleResolver;

                    let path_pos = expr.start_position();

                    let resolver = global.embedded_module_resolver.clone();

                    let module_result = resolver
                        .as_ref()
                        .and_then(|r| match r.resolve_raw(self, global, &path, path_pos) {
                            Err(err) if matches!(*err, ERR::ErrorModuleNotFound(..)) => None,
                            result => Some(result),
                        })
                        .or_else(|| {
                            Some(
                                self.module_resolver
                                    .resolve_raw(self, global, &path, path_pos),
                            )
                        })
                        .unwrap_or_else(|| {
                            Err(ERR::ErrorModuleNotFound(path.to_string(), path_pos).into())
                        });

                    if let Ok(module) = module_result {
                        if !export.is_empty() {
                            if module.is_indexed() {
                                global.push_import(export.name.clone(), 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.name.clone(), m);
                            }
                        }

                        global.num_modules_loaded += 1;

                        Ok(Dynamic::UNIT)
                    } else {
                        module_result.map(|_| Dynamic::UNIT)
                    }
                } else {
                    path_result.map(|_| Dynamic::UNIT)
                }
            }

            // Export statement
            #[cfg(not(feature = "no_module"))]
            Stmt::Export(x, ..) => {
                let (Ident { name, pos, .. }, Ident { name: alias, .. }) = &**x;
                // Mark scope variables as public
                if let Some((index, ..)) = scope.get_index(name) {
                    let alias = if alias.is_empty() { name } else { alias }.clone();
                    scope.add_alias_by_index(index, alias.into());
                    Ok(Dynamic::UNIT)
                } else {
                    Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into())
                }
            }

            // Share statement
            #[cfg(not(feature = "no_closure"))]
            Stmt::Share(name, pos) => {
                if let Some((index, ..)) = scope.get_index(name) {
                    let val = scope.get_mut_by_index(index);

                    if !val.is_shared() {
                        // Replace the variable with a shared value.
                        *val = std::mem::take(val).into_shared();
                    }
                    Ok(Dynamic::UNIT)
                } else {
                    Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into())
                }
            }

            _ => unreachable!("statement cannot be evaluated: {:?}", stmt),
        };

        #[cfg(feature = "debugging")]
        global.debugger.reset_status(reset_debugger);

        result
    }
}