use crate::{
exec::{Executable, InterpreterState},
gc::{Finalize, Trace},
syntax::ast::node::Node,
Context, JsResult, JsValue,
};
use std::fmt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct Continue {
label: Option<Box<str>>,
}
impl Continue {
pub fn label(&self) -> Option<&str> {
self.label.as_ref().map(Box::as_ref)
}
pub fn new<OL, L>(label: OL) -> Self
where
L: Into<Box<str>>,
OL: Into<Option<L>>,
{
Self {
label: label.into().map(L::into),
}
}
}
impl Executable for Continue {
fn run(&self, context: &mut Context) -> JsResult<JsValue> {
context
.executor()
.set_current_state(InterpreterState::Continue(self.label().map(Box::from)));
Ok(JsValue::undefined())
}
}
impl fmt::Display for Continue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "continue")?;
if let Some(label) = self.label() {
write!(f, " {}", label)?;
}
Ok(())
}
}
impl From<Continue> for Node {
fn from(cont: Continue) -> Node {
Self::Continue(cont)
}
}