boa/syntax/ast/node/iteration/continue_node/
mod.rs

1use crate::{
2    exec::{Executable, InterpreterState},
3    gc::{Finalize, Trace},
4    syntax::ast::node::Node,
5    Context, JsResult, JsValue,
6};
7use std::fmt;
8
9#[cfg(feature = "deser")]
10use serde::{Deserialize, Serialize};
11
12/// The `continue` statement terminates execution of the statements in the current iteration of
13/// the current or labeled loop, and continues execution of the loop with the next iteration.
14///
15/// The continue statement can include an optional label that allows the program to jump to the
16/// next iteration of a labeled loop statement instead of the current loop. In this case, the
17/// continue statement needs to be nested within this labeled statement.
18///
19/// More information:
20///  - [ECMAScript reference][spec]
21///  - [MDN documentation][mdn]
22///
23/// [spec]: https://tc39.es/ecma262/#prod-ContinueStatement
24/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue
25#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
26#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
27pub struct Continue {
28    label: Option<Box<str>>,
29}
30
31impl Continue {
32    pub fn label(&self) -> Option<&str> {
33        self.label.as_ref().map(Box::as_ref)
34    }
35
36    /// Creates a `Continue` AST node.
37    pub fn new<OL, L>(label: OL) -> Self
38    where
39        L: Into<Box<str>>,
40        OL: Into<Option<L>>,
41    {
42        Self {
43            label: label.into().map(L::into),
44        }
45    }
46}
47
48impl Executable for Continue {
49    fn run(&self, context: &mut Context) -> JsResult<JsValue> {
50        context
51            .executor()
52            .set_current_state(InterpreterState::Continue(self.label().map(Box::from)));
53
54        Ok(JsValue::undefined())
55    }
56}
57
58impl fmt::Display for Continue {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "continue")?;
61        if let Some(label) = self.label() {
62            write!(f, " {}", label)?;
63        }
64        Ok(())
65    }
66}
67
68impl From<Continue> for Node {
69    fn from(cont: Continue) -> Node {
70        Self::Continue(cont)
71    }
72}