-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
451 additions
and
94 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
boa/src/syntax/ast/node/declaration/async_arrow_function_decl/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::{ | ||
builtins::function::FunctionFlags, | ||
exec::Executable, | ||
gc::{Finalize, Trace}, | ||
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList}, | ||
Context, Result, Value, | ||
}; | ||
use std::fmt; | ||
|
||
#[cfg(feature = "deser")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// | ||
/// | ||
/// | ||
/// | ||
/// More information: | ||
/// - [ECMAScript reference][spec] | ||
/// - [MDN documentation][mdn] | ||
/// | ||
/// [spec]: | ||
/// [mdn]: | ||
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] | ||
#[derive(Clone, Debug, Trace, Finalize, PartialEq)] | ||
pub struct AsyncArrowFunctionDecl { | ||
params: Box<[FormalParameter]>, | ||
body: StatementList, | ||
} | ||
|
||
impl AsyncArrowFunctionDecl { | ||
/// Creates a new `AsyncArrowFunctionDecl` AST node. | ||
pub(in crate::syntax) fn new<P, B>(params: P, body: B) -> Self | ||
where | ||
P: Into<Box<[FormalParameter]>>, | ||
B: Into<StatementList>, | ||
{ | ||
Self { | ||
params: params.into(), | ||
body: body.into(), | ||
} | ||
} | ||
|
||
/// Gets the list of parameters of the arrow function. | ||
pub(crate) fn params(&self) -> &[FormalParameter] { | ||
&self.params | ||
} | ||
|
||
/// Gets the body of the arrow function. | ||
pub(crate) fn body(&self) -> &[Node] { | ||
&self.body.items() | ||
} | ||
|
||
/// Implements the display formatting with indentation. | ||
pub(in crate::syntax::ast::node) fn display( | ||
&self, | ||
f: &mut fmt::Formatter<'_>, | ||
indentation: usize, | ||
) -> fmt::Result { | ||
write!(f, "async (")?; | ||
join_nodes(f, &self.params)?; | ||
f.write_str(") => ")?; | ||
self.body.display(f, indentation) | ||
} | ||
} | ||
|
||
impl Executable for AsyncArrowFunctionDecl { | ||
fn run(&self, context: &mut Context) -> Result<Value> { | ||
Ok(context.create_function( | ||
self.params().to_vec(), | ||
self.body().to_vec(), | ||
FunctionFlags::CALLABLE | ||
| FunctionFlags::CONSTRUCTABLE | ||
| FunctionFlags::LEXICAL_THIS_MODE, | ||
)?) | ||
} | ||
} | ||
|
||
impl fmt::Display for AsyncArrowFunctionDecl { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.display(f, 0) | ||
} | ||
} | ||
|
||
impl From<AsyncArrowFunctionDecl> for Node { | ||
fn from(decl: AsyncArrowFunctionDecl) -> Self { | ||
Self::AsyncArrowFunctionDecl(decl) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
boa/src/syntax/parser/expression/assignment/async_arrow_function/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! Async Arrow function parsing. | ||
//! | ||
//! More information: | ||
//! - [MDN documentation][mdn] | ||
//! - [ECMAScript specification][spec] | ||
//! | ||
//! [mdn]: | ||
//! [spec]: https://tc39.es/ecma262/#prod-AsyncArrowFunction | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
use super::ConciseBody; | ||
use crate::{ | ||
syntax::{ | ||
ast::{ | ||
node::{AsyncArrowFunctionDecl, FormalParameter}, | ||
Keyword, Punctuator, | ||
}, | ||
lexer::TokenKind, | ||
parser::{ | ||
error::{ErrorContext, ParseError}, | ||
function::FormalParameters, | ||
statement::BindingIdentifier, | ||
AllowAwait, AllowIn, AllowYield, Cursor, TokenParser, | ||
}, | ||
}, | ||
BoaProfiler, | ||
}; | ||
|
||
use std::io::Read; | ||
|
||
/// Arrow function parsing. | ||
/// | ||
/// More information: | ||
/// - [MDN documentation][mdn] | ||
/// - [ECMAScript specification][spec] | ||
/// | ||
/// [mdn]: | ||
/// [spec]: https://tc39.es/ecma262/#prod-AsyncArrowFunction | ||
#[derive(Debug, Clone, Copy)] | ||
pub(in crate::syntax::parser) struct AsyncArrowFunction { | ||
allow_in: AllowIn, | ||
allow_yield: AllowYield, | ||
allow_await: AllowAwait, | ||
} | ||
|
||
impl AsyncArrowFunction { | ||
/// Creates a new `AsyncArrowFunction` parser. | ||
pub(in crate::syntax::parser) fn new<I, Y, A>( | ||
allow_in: I, | ||
allow_yield: Y, | ||
allow_await: A, | ||
) -> Self | ||
where | ||
I: Into<AllowIn>, | ||
Y: Into<AllowYield>, | ||
A: Into<AllowAwait>, | ||
{ | ||
Self { | ||
allow_in: allow_in.into(), | ||
allow_yield: allow_yield.into(), | ||
allow_await: allow_await.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<R> TokenParser<R> for AsyncArrowFunction | ||
where | ||
R: Read, | ||
{ | ||
type Output = AsyncArrowFunctionDecl; | ||
|
||
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> { | ||
let _timer = BoaProfiler::global().start_event("AsyncArrowFunction", "Parsing"); | ||
cursor.expect( | ||
TokenKind::Keyword(Keyword::Async), | ||
"async arrow function parsing", | ||
)?; | ||
let token = cursor.peek_expect_no_lineterminator(0, "async arrow function parsing")?; | ||
|
||
let params = if let TokenKind::Punctuator(Punctuator::OpenParen) = &token.kind() { | ||
// CoverCallExpressionAndAsyncArrowHead | ||
cursor.expect(Punctuator::OpenParen, "async arrow function")?; | ||
|
||
let params = FormalParameters::new(false, true, true).parse(cursor)?; | ||
cursor.expect(Punctuator::CloseParen, "async arrow function")?; | ||
params | ||
} else { | ||
// AsyncArrowBindingIdentifier | ||
let param = BindingIdentifier::new(self.allow_yield, true) | ||
.parse(cursor) | ||
.context("async arrow function")?; | ||
Box::new([FormalParameter::new(param, None, false)]) | ||
}; | ||
|
||
cursor.peek_expect_no_lineterminator(0, "async arrow function")?; | ||
cursor.expect( | ||
TokenKind::Punctuator(Punctuator::Arrow), | ||
"async arrow function", | ||
)?; | ||
let body = ConciseBody::new(self.allow_in, true).parse(cursor)?; | ||
Ok(AsyncArrowFunctionDecl::new(params, body)) | ||
} | ||
} |
Oops, something went wrong.