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
//! Iteration node execution.

use super::{Executable, Interpreter, InterpreterState};
use crate::{
    builtins::value::{ResultValue, Value},
    environment::lexical_environment::new_declarative_environment,
    syntax::ast::node::{DoWhileLoop, ForLoop, WhileLoop},
    BoaProfiler,
};
use std::borrow::Borrow;

#[cfg(test)]
mod tests;

impl Executable for ForLoop {
    fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
        // Create the block environment.
        let _timer = BoaProfiler::global().start_event("ForLoop", "exec");
        {
            let env = &mut interpreter.realm_mut().environment;
            env.push(new_declarative_environment(Some(
                env.get_current_environment_ref().clone(),
            )));
        }

        if let Some(init) = self.init() {
            init.run(interpreter)?;
        }

        while self
            .condition()
            .map(|cond| cond.run(interpreter).map(|v| v.is_true()))
            .transpose()?
            .unwrap_or(true)
        {
            let result = self.body().run(interpreter)?;

            match interpreter.get_current_state() {
                InterpreterState::Break(_label) => {
                    // TODO break to label.

                    // Loops 'consume' breaks.
                    interpreter.set_current_state(InterpreterState::Executing);
                    break;
                }
                InterpreterState::Return => {
                    return Ok(result);
                }
                _ => {
                    // Continue execution.
                }
            }

            if let Some(final_expr) = self.final_expr() {
                final_expr.run(interpreter)?;
            }
        }

        // pop the block env
        let _ = interpreter.realm_mut().environment.pop();

        Ok(Value::undefined())
    }
}

impl Executable for WhileLoop {
    fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
        let mut result = Value::undefined();
        while self.cond().run(interpreter)?.borrow().is_true() {
            result = self.expr().run(interpreter)?;
            match interpreter.get_current_state() {
                InterpreterState::Break(_label) => {
                    // TODO break to label.

                    // Loops 'consume' breaks.
                    interpreter.set_current_state(InterpreterState::Executing);
                    break;
                }
                InterpreterState::Return => {
                    return Ok(result);
                }
                _ => {
                    // Continue execution.
                }
            }
        }
        Ok(result)
    }
}

impl Executable for DoWhileLoop {
    fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
        let mut result = self.body().run(interpreter)?;
        match interpreter.get_current_state() {
            InterpreterState::Break(_label) => {
                // TODO break to label.

                // Loops 'consume' breaks.
                interpreter.set_current_state(InterpreterState::Executing);
                return Ok(result);
            }
            InterpreterState::Return => {
                return Ok(result);
            }
            _ => {
                // Continue execution.
            }
        }

        while self.cond().run(interpreter)?.borrow().is_true() {
            result = self.body().run(interpreter)?;
            match interpreter.get_current_state() {
                InterpreterState::Break(_label) => {
                    // TODO break to label.

                    // Loops 'consume' breaks.
                    interpreter.set_current_state(InterpreterState::Executing);
                    break;
                }
                InterpreterState::Return => {
                    return Ok(result);
                }
                _ => {
                    // Continue execution.
                }
            }
        }
        Ok(result)
    }
}