Skip to content

Commit

Permalink
Started with the spans for AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Jun 11, 2021
1 parent 33a6965 commit 04f1be4
Show file tree
Hide file tree
Showing 53 changed files with 543 additions and 358 deletions.
41 changes: 20 additions & 21 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
statement_list::RcStatementList, Call, FormalParameter, Identifier, New,
StatementList,
},
Const, Node,
Const, Node, NodeKind, Span,
},
Parser,
},
Expand Down Expand Up @@ -336,8 +336,8 @@ impl Context {
{
// Runs a `new RangeError(message)`.
New::from(Call::new(
Identifier::from("RangeError"),
vec![Const::from(message.into()).into()],
Node::new(Identifier::from("RangeError"), Span::default()),
vec![Node::new(Const::from(message.into()), Span::default())],
))
.run(self)
.expect("Into<String> used as message")
Expand All @@ -360,8 +360,8 @@ impl Context {
{
// Runs a `new TypeError(message)`.
New::from(Call::new(
Identifier::from("TypeError"),
vec![Const::from(message.into()).into()],
Node::new(Identifier::from("TypeError"), Span::default()),
vec![Node::new(Const::from(message.into()), Span::default())],
))
.run(self)
.expect("Into<String> used as message")
Expand All @@ -383,8 +383,8 @@ impl Context {
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("ReferenceError"),
vec![Const::from(message.into()).into()],
Node::new(Identifier::from("ReferenceError"), Span::default()),
vec![Node::new(Const::from(message.into()), Span::default())],
))
.run(self)
.expect("Into<String> used as message")
Expand All @@ -406,8 +406,8 @@ impl Context {
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("SyntaxError"),
vec![Const::from(message.into()).into()],
Node::new(Identifier::from("SyntaxError"), Span::default()),
vec![Node::new(Const::from(message.into()), Span::default())],
))
.run(self)
.expect("Into<String> used as message")
Expand All @@ -428,8 +428,8 @@ impl Context {
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("EvalError"),
vec![Const::from(message.into()).into()],
Node::new(Identifier::from("EvalError"), Span::default()),
vec![Node::new(Const::from(message.into()), Span::default())],
))
.run(self)
.expect("Into<String> used as message")
Expand All @@ -441,8 +441,8 @@ impl Context {
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("URIError"),
vec![Const::from(message.into()).into()],
Node::new(Identifier::from("URIError"), Span::default()),
vec![Node::new(Const::from(message.into()), Span::default())],
))
.run(self)
.expect("Into<String> used as message")
Expand All @@ -465,15 +465,14 @@ impl Context {
}

/// Utility to create a function Value for Function Declarations, Arrow Functions or Function Expressions
pub(crate) fn create_function<P, B>(
pub(crate) fn create_function<P>(
&mut self,
params: P,
body: B,
body: StatementList,
flags: FunctionFlags,
) -> Result<Value>
where
P: Into<Box<[FormalParameter]>>,
B: Into<StatementList>,
{
let function_prototype: Value =
self.standard_objects().function_object().prototype().into();
Expand All @@ -485,7 +484,7 @@ impl Context {
let params_len = params.len();
let func = Function::Ordinary {
flags,
body: RcStatementList::from(body.into()),
body: RcStatementList::from(body),
params,
environment: self.get_current_environment().clone(),
};
Expand Down Expand Up @@ -549,16 +548,16 @@ impl Context {

#[inline]
pub(crate) fn set_value(&mut self, node: &Node, value: Value) -> Result<Value> {
match node {
Node::Identifier(ref name) => {
match node.kind() {
NodeKind::Identifier(ref name) => {
self.set_mutable_binding(name.as_ref(), value.clone(), true)?;
Ok(value)
}
Node::GetConstField(ref get_const_field_node) => Ok(get_const_field_node
NodeKind::GetConstField(ref get_const_field_node) => Ok(get_const_field_node
.obj()
.run(self)?
.set_field(get_const_field_node.field(), value, self)?),
Node::GetField(ref get_field) => {
NodeKind::GetField(ref get_field) => {
let field = get_field.field().run(self)?;
let key = field.to_property_key(self)?;
Ok(get_field.obj().run(self)?.set_field(key, value, self)?)
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod punctuator;
pub use self::{
constant::Const,
keyword::Keyword,
node::Node,
node::{Node, NodeKind},
position::{Position, Span},
punctuator::Punctuator,
};
8 changes: 4 additions & 4 deletions boa/src/syntax/ast/node/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Array declaration node.
use super::{join_nodes, Node};
use super::{join_nodes, Node, NodeKind};
use crate::{
builtins::{iterable, Array},
exec::Executable,
Expand Down Expand Up @@ -40,8 +40,8 @@ impl Executable for ArrayDecl {
let _timer = BoaProfiler::global().start_event("ArrayDecl", "exec");
let array = Array::new_array(context);
let mut elements = Vec::new();
for elem in self.as_ref() {
if let Node::Spread(ref x) = elem {
for elem in self.arr.iter().map(Node::kind) {
if let NodeKind::Spread(ref x) = elem {
let val = x.run(context)?;
let iterator_record = iterable::get_iterator(context, val)?;
// TODO after proper internal Array representation as per https://github.com/boa-dev/boa/pull/811#discussion_r502460858
Expand Down Expand Up @@ -89,7 +89,7 @@ impl fmt::Display for ArrayDecl {
}
}

impl From<ArrayDecl> for Node {
impl From<ArrayDecl> for NodeKind {
fn from(arr: ArrayDecl) -> Self {
Self::ArrayDecl(arr)
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/syntax/ast/node/await_expr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Await expression node.
use super::Node;
use super::{Node, NodeKind};
use crate::{exec::Executable, BoaProfiler, Context, Result, Value};
use gc::{Finalize, Trace};
use std::fmt;
Expand Down Expand Up @@ -54,7 +54,7 @@ impl fmt::Display for AwaitExpr {
}
}

impl From<AwaitExpr> for Node {
impl From<AwaitExpr> for NodeKind {
fn from(awaitexpr: AwaitExpr) -> Self {
Self::AwaitExpr(awaitexpr)
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/syntax/ast/node/block/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Block AST node.
use super::{Node, StatementList};
use super::{Node, NodeKind, StatementList};
use crate::{
environment::declarative_environment_record::DeclarativeEnvironmentRecord,
exec::Executable,
Expand Down Expand Up @@ -116,7 +116,7 @@ impl fmt::Display for Block {
}
}

impl From<Block> for Node {
impl From<Block> for NodeKind {
fn from(block: Block) -> Self {
Self::Block(block)
}
Expand Down
6 changes: 3 additions & 3 deletions boa/src/syntax/ast/node/break_node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Node;
use super::NodeKind;
use crate::{
exec::Executable,
exec::InterpreterState,
Expand Down Expand Up @@ -75,8 +75,8 @@ impl fmt::Display for Break {
}
}

impl From<Break> for Node {
fn from(break_smt: Break) -> Node {
impl From<Break> for NodeKind {
fn from(break_smt: Break) -> Self {
Self::Break(break_smt)
}
}
14 changes: 7 additions & 7 deletions boa/src/syntax/ast/node/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
exec::Executable,
exec::InterpreterState,
gc::{Finalize, Trace},
syntax::ast::node::{join_nodes, Node},
syntax::ast::node::{join_nodes, Node, NodeKind},
value::{Type, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -60,8 +60,8 @@ impl Call {
impl Executable for Call {
fn run(&self, context: &mut Context) -> Result<Value> {
let _timer = BoaProfiler::global().start_event("Call", "exec");
let (this, func) = match self.expr() {
Node::GetConstField(ref get_const_field) => {
let (this, func) = match self.expr().kind() {
NodeKind::GetConstField(ref get_const_field) => {
let mut obj = get_const_field.obj().run(context)?;
if obj.get_type() != Type::Object {
obj = Value::Object(obj.to_object(context)?);
Expand All @@ -71,7 +71,7 @@ impl Executable for Call {
obj.get_field(get_const_field.field(), context)?,
)
}
Node::GetField(ref get_field) => {
NodeKind::GetField(ref get_field) => {
let mut obj = get_field.obj().run(context)?;
if obj.get_type() != Type::Object {
obj = Value::Object(obj.to_object(context)?);
Expand All @@ -89,8 +89,8 @@ impl Executable for Call {
),
};
let mut v_args = Vec::with_capacity(self.args().len());
for arg in self.args() {
if let Node::Spread(ref x) = arg {
for arg in self.args().iter().map(Node::kind) {
if let NodeKind::Spread(ref x) = arg {
let val = x.run(context)?;
let iterator_record = iterable::get_iterator(context, val)?;
loop {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl fmt::Display for Call {
}
}

impl From<Call> for Node {
impl From<Call> for NodeKind {
fn from(call: Call) -> Self {
Self::Call(call)
}
Expand Down
6 changes: 3 additions & 3 deletions boa/src/syntax/ast/node/conditional/conditional_op/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::node::Node,
syntax::ast::node::{Node, NodeKind},
Context, Result, Value,
};
use std::fmt;
Expand Down Expand Up @@ -81,8 +81,8 @@ impl fmt::Display for ConditionalOp {
}
}

impl From<ConditionalOp> for Node {
fn from(cond_op: ConditionalOp) -> Node {
impl From<ConditionalOp> for NodeKind {
fn from(cond_op: ConditionalOp) -> Self {
Self::ConditionalOp(cond_op)
}
}
6 changes: 3 additions & 3 deletions boa/src/syntax/ast/node/conditional/if_node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::node::Node,
syntax::ast::node::{Node, NodeKind},
Context, Result, Value,
};
use std::fmt;
Expand Down Expand Up @@ -96,8 +96,8 @@ impl fmt::Display for If {
}
}

impl From<If> for Node {
fn from(if_stm: If) -> Node {
impl From<If> for NodeKind {
fn from(if_stm: If) -> Self {
Self::If(if_stm)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
builtins::function::FunctionFlags,
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList},
syntax::ast::node::{join_nodes, FormalParameter, Node, NodeKind, StatementList},
Context, Result, Value,
};
use std::fmt;
Expand Down Expand Up @@ -69,8 +69,8 @@ impl ArrowFunctionDecl {
impl Executable for ArrowFunctionDecl {
fn run(&self, context: &mut Context) -> Result<Value> {
context.create_function(
self.params().to_vec(),
self.body().to_vec(),
self.params.clone(),
self.body.clone(),
FunctionFlags::CALLABLE
| FunctionFlags::CONSTRUCTABLE
| FunctionFlags::LEXICAL_THIS_MODE,
Expand All @@ -84,7 +84,7 @@ impl fmt::Display for ArrowFunctionDecl {
}
}

impl From<ArrowFunctionDecl> for Node {
impl From<ArrowFunctionDecl> for NodeKind {
fn from(decl: ArrowFunctionDecl) -> Self {
Self::ArrowFunctionDecl(decl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
exec::Executable,
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList},
syntax::ast::node::{join_nodes, FormalParameter, Node, NodeKind, StatementList},
BoaProfiler, Context, Result, Value,
};
use gc::{Finalize, Trace};
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Executable for AsyncFunctionDecl {
}
}

impl From<AsyncFunctionDecl> for Node {
impl From<AsyncFunctionDecl> for NodeKind {
fn from(decl: AsyncFunctionDecl) -> Self {
Self::AsyncFunctionDecl(decl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
exec::Executable,
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList},
syntax::ast::node::{join_nodes, FormalParameter, Node, NodeKind, StatementList},
Context, Result, Value,
};
use gc::{Finalize, Trace};
Expand Down Expand Up @@ -91,7 +91,7 @@ impl fmt::Display for AsyncFunctionExpr {
}
}

impl From<AsyncFunctionExpr> for Node {
impl From<AsyncFunctionExpr> for NodeKind {
fn from(expr: AsyncFunctionExpr) -> Self {
Self::AsyncFunctionExpr(expr)
}
Expand Down
8 changes: 4 additions & 4 deletions boa/src/syntax/ast/node/declaration/function_decl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
environment::lexical_environment::VariableScope,
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList},
syntax::ast::node::{join_nodes, FormalParameter, Node, NodeKind, StatementList},
BoaProfiler, Context, Result, Value,
};
use std::fmt;
Expand Down Expand Up @@ -87,8 +87,8 @@ impl Executable for FunctionDecl {
fn run(&self, context: &mut Context) -> Result<Value> {
let _timer = BoaProfiler::global().start_event("FunctionDecl", "exec");
let val = context.create_function(
self.parameters().to_vec(),
self.body().to_vec(),
self.parameters.clone(),
self.body.clone(),
FunctionFlags::CALLABLE | FunctionFlags::CONSTRUCTABLE,
)?;

Expand All @@ -110,7 +110,7 @@ impl Executable for FunctionDecl {
}
}

impl From<FunctionDecl> for Node {
impl From<FunctionDecl> for NodeKind {
fn from(decl: FunctionDecl) -> Self {
Self::FunctionDecl(decl)
}
Expand Down
Loading

0 comments on commit 04f1be4

Please sign in to comment.