1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
use crate::expression::Call;
use crate::visitor::{VisitWith, Visitor, VisitorMut};
use boa_interner::{Interner, ToInternedString};
use core::ops::ControlFlow;
use super::Expression;
/// The `new` operator lets developers create an instance of a user-defined object type or of
/// one of the built-in object types that has a constructor function.
///
/// The new keyword does the following things:
/// - Creates a blank, plain JavaScript object;
/// - Links (sets the constructor of) this object to another object;
/// - Passes the newly created object from Step 1 as the this context;
/// - Returns this if the function doesn't return its own object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#prod-NewExpression
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct New {
call: Call,
}
impl New {
/// Gets the constructor of the new expression.
#[inline]
#[must_use]
pub const fn constructor(&self) -> &Expression {
self.call.function()
}
/// Retrieves the arguments passed to the constructor.
#[inline]
#[must_use]
pub const fn arguments(&self) -> &[Expression] {
self.call.args()
}
/// Returns the inner call expression.
#[must_use]
pub const fn call(&self) -> &Call {
&self.call
}
}
impl From<Call> for New {
#[inline]
fn from(call: Call) -> Self {
Self { call }
}
}
impl ToInternedString for New {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
format!("new {}", self.call.to_interned_string(interner))
}
}
impl From<New> for Expression {
#[inline]
fn from(new: New) -> Self {
Self::New(new)
}
}
impl VisitWith for New {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
visitor.visit_call(&self.call)
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
visitor.visit_call_mut(&mut self.call)
}
}