use crate::{
builtins::iterable,
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::node::{Call, Node},
value::JsValue,
BoaProfiler, Context, JsResult,
};
use std::fmt;
#[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 New {
call: Call,
}
impl New {
pub fn expr(&self) -> &Node {
self.call.expr()
}
pub fn args(&self) -> &[Node] {
self.call.args()
}
}
impl Executable for New {
fn run(&self, context: &mut Context) -> JsResult<JsValue> {
let _timer = BoaProfiler::global().start_event("New", "exec");
let func_object = self.expr().run(context)?;
let mut v_args = Vec::with_capacity(self.args().len());
for arg in self.args() {
if let Node::Spread(ref x) = arg {
let val = x.run(context)?;
let iterator_record = iterable::get_iterator(&val, context)?;
loop {
let next = iterator_record.next(context)?;
if next.done {
break;
}
let next_value = next.value;
v_args.push(next_value);
}
break; } else {
v_args.push(arg.run(context)?);
}
}
match func_object {
JsValue::Object(ref object) => {
object.construct(&v_args, &object.clone().into(), context)
}
_ => context
.throw_type_error(format!("{} is not a constructor", self.expr().to_string(),)),
}
}
}
impl From<Call> for New {
fn from(call: Call) -> Self {
Self { call }
}
}
impl fmt::Display for New {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "new {}", self.call)
}
}
impl From<New> for Node {
fn from(new: New) -> Self {
Self::New(new)
}
}