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
88
89
90
91
92
93
94
95
use crate::{
builtins::function::FunctionFlags,
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};
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub struct ArrowFunctionDecl {
params: Box<[FormalParameter]>,
body: StatementList,
}
impl ArrowFunctionDecl {
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(),
}
}
pub(crate) fn params(&self) -> &[FormalParameter] {
&self.params
}
pub(crate) fn body(&self) -> &StatementList {
&self.body
}
pub(in crate::syntax::ast::node) fn display(
&self,
f: &mut fmt::Formatter<'_>,
indentation: usize,
) -> fmt::Result {
write!(f, "(")?;
join_nodes(f, &self.params)?;
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 ArrowFunctionDecl {
fn run(&self, context: &mut Context) -> JsResult<JsValue> {
context.create_function(
"",
self.params().to_vec(),
self.body().clone(),
FunctionFlags::LEXICAL_THIS_MODE,
)
}
}
impl fmt::Display for ArrowFunctionDecl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.display(f, 0)
}
}
impl From<ArrowFunctionDecl> for Node {
fn from(decl: ArrowFunctionDecl) -> Self {
Self::ArrowFunctionDecl(decl)
}
}