-
-
Notifications
You must be signed in to change notification settings - Fork 415
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
32 changed files
with
1,605 additions
and
373 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::{ | ||
exec::Executable, | ||
gc::{Finalize, Trace}, | ||
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList}, | ||
BoaProfiler, Context, JsResult, JsValue, | ||
}; | ||
use std::fmt; | ||
|
||
#[cfg(feature = "deser")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// The `function*` declaration (`function` keyword followed by an asterisk) defines a generator function, | ||
/// which returns a `Generator` object. | ||
/// | ||
/// More information: | ||
/// - [ECMAScript reference][spec] | ||
/// - [MDN documentation][mdn] | ||
/// | ||
/// [spec]: https://tc39.es/ecma262/#prod-GeneratorDeclaration | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* | ||
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] | ||
#[derive(Clone, Debug, Trace, Finalize, PartialEq)] | ||
pub struct GeneratorDecl { | ||
name: Box<str>, | ||
parameters: Box<[FormalParameter]>, | ||
body: StatementList, | ||
} | ||
|
||
impl GeneratorDecl { | ||
/// Creates a new generator declaration. | ||
pub(in crate::syntax) fn new<N, P, B>(name: N, parameters: P, body: B) -> Self | ||
where | ||
N: Into<Box<str>>, | ||
P: Into<Box<[FormalParameter]>>, | ||
B: Into<StatementList>, | ||
{ | ||
Self { | ||
name: name.into(), | ||
parameters: parameters.into(), | ||
body: body.into(), | ||
} | ||
} | ||
|
||
/// Gets the name of the generator declaration. | ||
pub fn name(&self) -> &str { | ||
&self.name | ||
} | ||
|
||
/// Gets the list of parameters of the generator declaration. | ||
pub fn parameters(&self) -> &[FormalParameter] { | ||
&self.parameters | ||
} | ||
|
||
/// Gets the body of the generator declaration. | ||
pub 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, "function* {}(", self.name)?; | ||
join_nodes(f, &self.parameters)?; | ||
if self.body().is_empty() { | ||
f.write_str(") {}") | ||
} else { | ||
f.write_str(") {\n")?; | ||
self.body.display(f, indentation + 1)?; | ||
write!(f, "{}}}", " ".repeat(indentation)) | ||
} | ||
} | ||
} | ||
|
||
impl Executable for GeneratorDecl { | ||
fn run(&self, _context: &mut Context) -> JsResult<JsValue> { | ||
let _timer = BoaProfiler::global().start_event("GeneratorDecl", "exec"); | ||
// TODO: Implement GeneratorFunction | ||
// https://tc39.es/ecma262/#sec-generatorfunction-objects | ||
Ok(JsValue::undefined()) | ||
} | ||
} | ||
|
||
impl From<GeneratorDecl> for Node { | ||
fn from(decl: GeneratorDecl) -> Self { | ||
Self::GeneratorDecl(decl) | ||
} | ||
} | ||
|
||
impl fmt::Display for GeneratorDecl { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.display(f, 0) | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
boa/src/syntax/ast/node/declaration/generator_expr/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,109 @@ | ||
use crate::{ | ||
exec::Executable, | ||
gc::{Finalize, Trace}, | ||
syntax::ast::node::{join_nodes, FormalParameter, Node, StatementList}, | ||
Context, JsResult, JsValue, | ||
}; | ||
use std::fmt; | ||
|
||
#[cfg(feature = "deser")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// The `function*` keyword can be used to define a generator function inside an expression. | ||
/// | ||
/// More information: | ||
/// - [ECMAScript reference][spec] | ||
/// - [MDN documentation][mdn] | ||
/// | ||
/// [spec]: https://tc39.es/ecma262/#prod-GeneratorExpression | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function* | ||
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] | ||
#[derive(Clone, Debug, Trace, Finalize, PartialEq)] | ||
pub struct GeneratorExpr { | ||
name: Option<Box<str>>, | ||
parameters: Box<[FormalParameter]>, | ||
body: StatementList, | ||
} | ||
|
||
impl GeneratorExpr { | ||
/// Creates a new generator expression | ||
pub(in crate::syntax) fn new<N, P, B>(name: N, parameters: P, body: B) -> Self | ||
where | ||
N: Into<Option<Box<str>>>, | ||
P: Into<Box<[FormalParameter]>>, | ||
B: Into<StatementList>, | ||
{ | ||
Self { | ||
name: name.into(), | ||
parameters: parameters.into(), | ||
body: body.into(), | ||
} | ||
} | ||
|
||
/// Gets the name of the generator declaration. | ||
pub fn name(&self) -> Option<&str> { | ||
self.name.as_ref().map(Box::as_ref) | ||
} | ||
|
||
/// Gets the list of parameters of the generator declaration. | ||
pub fn parameters(&self) -> &[FormalParameter] { | ||
&self.parameters | ||
} | ||
|
||
/// Gets the body of the generator declaration. | ||
pub fn body(&self) -> &StatementList { | ||
&self.body | ||
} | ||
|
||
/// Implements the display formatting with indentation. | ||
pub(in crate::syntax::ast::node) fn display( | ||
&self, | ||
f: &mut fmt::Formatter<'_>, | ||
indentation: usize, | ||
) -> fmt::Result { | ||
f.write_str("function*")?; | ||
if let Some(ref name) = self.name { | ||
write!(f, " {}", name)?; | ||
} | ||
f.write_str("(")?; | ||
join_nodes(f, &self.parameters)?; | ||
f.write_str(") ")?; | ||
self.display_block(f, indentation) | ||
} | ||
|
||
/// Displays the generator's body. This includes the curly braces at the start and end. | ||
/// This will not indent the first brace, but will indent the last brace. | ||
pub(in crate::syntax::ast::node) fn display_block( | ||
&self, | ||
f: &mut fmt::Formatter<'_>, | ||
indentation: usize, | ||
) -> fmt::Result { | ||
if self.body().items().is_empty() { | ||
f.write_str("{}") | ||
} else { | ||
f.write_str("{\n")?; | ||
self.body.display(f, indentation + 1)?; | ||
write!(f, "{}}}", " ".repeat(indentation)) | ||
} | ||
} | ||
} | ||
|
||
impl Executable for GeneratorExpr { | ||
fn run(&self, _context: &mut Context) -> JsResult<JsValue> { | ||
// TODO: Implement GeneratorFunction | ||
// https://tc39.es/ecma262/#sec-generatorfunction-objects | ||
Ok(JsValue::undefined()) | ||
} | ||
} | ||
|
||
impl fmt::Display for GeneratorExpr { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.display(f, 0) | ||
} | ||
} | ||
|
||
impl From<GeneratorExpr> for Node { | ||
fn from(expr: GeneratorExpr) -> Self { | ||
Self::GeneratorExpr(expr) | ||
} | ||
} |
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
Oops, something went wrong.