Skip to main content

boa_ast/statement/
throw.rs

1use crate::{
2    Expression,
3    statement::Statement,
4    visitor::{VisitWith, Visitor, VisitorMut},
5};
6use boa_interner::{Interner, ToInternedString};
7use core::ops::ControlFlow;
8
9/// The `throw` statement throws a user-defined exception.
10///
11/// Syntax: `throw expression;`
12///
13/// Execution of the current function will stop (the statements after throw won't be executed),
14/// and control will be passed to the first catch block in the call stack. If no catch block
15/// exists among caller functions, the program will terminate.
16///
17/// More information:
18///  - [ECMAScript reference][spec]
19///  - [MDN documentation][mdn]
20///
21/// [spec]: https://tc39.es/ecma262/#prod-ThrowStatement
22/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
25#[derive(Clone, Debug, PartialEq)]
26pub struct Throw {
27    target: Expression,
28}
29
30impl Throw {
31    /// Gets the target expression of this `Throw` statement.
32    #[must_use]
33    pub const fn target(&self) -> &Expression {
34        &self.target
35    }
36
37    /// Creates a `Throw` AST node.
38    #[must_use]
39    pub const fn new(target: Expression) -> Self {
40        Self { target }
41    }
42}
43
44impl ToInternedString for Throw {
45    fn to_interned_string(&self, interner: &Interner) -> String {
46        format!("throw {}", self.target.to_interned_string(interner))
47    }
48}
49
50impl From<Throw> for Statement {
51    fn from(trw: Throw) -> Self {
52        Self::Throw(trw)
53    }
54}
55
56impl VisitWith for Throw {
57    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
58    where
59        V: Visitor<'a>,
60    {
61        visitor.visit_expression(&self.target)
62    }
63
64    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
65    where
66        V: VisitorMut<'a>,
67    {
68        visitor.visit_expression_mut(&mut self.target)
69    }
70}