use crate::{
exec::{Executable, InterpreterState},
gc::{Finalize, Trace},
syntax::ast::node::Node,
Context, JsResult, JsValue,
};
use std::fmt;
use crate::syntax::ast::node::StatementList;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};
#[cfg(test)]
mod tests;
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct Case {
condition: Node,
body: StatementList,
}
impl Case {
pub fn new<C, B>(condition: C, body: B) -> Self
where
C: Into<Node>,
B: Into<StatementList>,
{
Self {
condition: condition.into(),
body: body.into(),
}
}
pub fn condition(&self) -> &Node {
&self.condition
}
pub fn body(&self) -> &StatementList {
&self.body
}
}
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct Switch {
val: Box<Node>,
cases: Box<[Case]>,
default: Option<StatementList>,
}
impl Switch {
pub fn new<V, C, D>(val: V, cases: C, default: Option<D>) -> Self
where
V: Into<Node>,
C: Into<Box<[Case]>>,
D: Into<StatementList>,
{
Self {
val: Box::new(val.into()),
cases: cases.into(),
default: default.map(D::into),
}
}
pub fn val(&self) -> &Node {
&self.val
}
pub fn cases(&self) -> &[Case] {
&self.cases
}
pub fn default(&self) -> Option<&[Node]> {
self.default.as_ref().map(StatementList::items)
}
pub(in crate::syntax::ast::node) fn display(
&self,
f: &mut fmt::Formatter<'_>,
indentation: usize,
) -> fmt::Result {
let indent = " ".repeat(indentation);
writeln!(f, "switch ({}) {{", self.val())?;
for e in self.cases().iter() {
writeln!(f, "{} case {}:", indent, e.condition())?;
e.body().display(f, indentation + 2)?;
}
if let Some(ref default) = self.default {
writeln!(f, "{} default:", indent)?;
default.display(f, indentation + 2)?;
}
write!(f, "{}}}", indent)
}
}
impl Executable for Switch {
fn run(&self, context: &mut Context) -> JsResult<JsValue> {
let val = self.val().run(context)?;
let mut result = JsValue::null();
let mut matched = false;
context
.executor()
.set_current_state(InterpreterState::Executing);
let mut fall_through: bool = false;
for case in self.cases().iter() {
let cond = case.condition();
let block = case.body();
if fall_through || val.strict_equals(&cond.run(context)?) {
matched = true;
let result = block.run(context)?;
match context.executor().get_current_state() {
InterpreterState::Return => {
return Ok(result);
}
InterpreterState::Break(_label) => {
context
.executor()
.set_current_state(InterpreterState::Executing);
break;
}
InterpreterState::Continue(_label) => {
break;
}
InterpreterState::Executing => {
fall_through = true;
}
}
}
}
if !matched {
if let Some(default) = self.default() {
context
.executor()
.set_current_state(InterpreterState::Executing);
for (i, item) in default.iter().enumerate() {
let val = item.run(context)?;
match context.executor().get_current_state() {
InterpreterState::Return => {
result = val;
break;
}
InterpreterState::Break(_label) => {
break;
}
_ => {
}
}
if i == default.len() - 1 {
result = val;
}
}
}
}
Ok(result)
}
}
impl fmt::Display for Switch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.display(f, 0)
}
}
impl From<Switch> for Node {
fn from(switch: Switch) -> Self {
Self::Switch(switch)
}
}