Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ast/node/expression into ast/node/call and ast/node/new #542

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boa/src/exec/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
impl Executable for Call {
fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
let _timer = BoaProfiler::global().start_event("Call", "exec");

let (this, func) = match self.expr() {
Node::GetConstField(ref get_const_field) => {
let mut obj = get_const_field.obj().run(interpreter)?;
Expand Down
4 changes: 0 additions & 4 deletions boa/src/exec/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ use crate::{
impl Executable for New {
fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
let _timer = BoaProfiler::global().start_event("New", "exec");
// let (callee, args) = match call.as_ref() {
// Node::Call(callee, args) => (callee, args),
// _ => unreachable!("Node::New(ref call): 'call' must only be Node::Call type."),
// };

let func_object = self.expr().run(interpreter)?;
let mut v_args = Vec::with_capacity(self.args().len());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! Expression nodes.

use super::{join_nodes, Node};
use gc::{Finalize, Trace};
use std::fmt;
Expand Down Expand Up @@ -65,54 +63,3 @@ impl From<Call> for Node {
Self::Call(call)
}
}

/// 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(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct New {
call: Call,
}

impl New {
/// Gets the name of the function call.
pub fn expr(&self) -> &Node {
&self.call.expr()
}

/// Retrieves the arguments passed to the function.
pub fn args(&self) -> &[Node] {
&self.call.args()
}
}

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)
}
}
6 changes: 4 additions & 2 deletions boa/src/syntax/ast/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
pub mod array;
pub mod block;
pub mod break_node;
pub mod call;
pub mod conditional;
pub mod declaration;
pub mod expression;
pub mod field;
pub mod identifier;
pub mod iteration;
pub mod new;
pub mod object;
pub mod operator;
pub mod return_smt;
Expand All @@ -22,15 +23,16 @@ pub use self::{
array::ArrayDecl,
block::Block,
break_node::Break,
call::Call,
conditional::{ConditionalOp, If},
declaration::{
ArrowFunctionDecl, ConstDecl, ConstDeclList, FunctionDecl, FunctionExpr, LetDecl,
LetDeclList, VarDecl, VarDeclList,
},
expression::{Call, New},
field::{GetConstField, GetField},
identifier::Identifier,
iteration::{Continue, DoWhileLoop, ForLoop, WhileLoop},
new::New,
object::Object,
operator::{Assign, BinOp, UnaryOp},
return_smt::Return,
Expand Down
59 changes: 59 additions & 0 deletions boa/src/syntax/ast/node/new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::Node;
use gc::{Finalize, Trace};
use std::fmt;

pub use super::call::Call;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// 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(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct New {
call: Call,
}

impl New {
/// Gets the name of the function call.
pub fn expr(&self) -> &Node {
&self.call.expr()
}

/// Retrieves the arguments passed to the function.
pub fn args(&self) -> &[Node] {
&self.call.args()
}
}

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)
}
}