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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use ast::*;
use EnrichError;
use binjs_shared::{Offset, VisitMe};
use std;
use std::cell::RefCell;
use std::rc::Rc;
type EnterResult = Result<VisitMe<Option<LevelGuard>>, EnrichError>;
type ExitResult<T> = Result<Option<T>, EnrichError>;
pub struct LevelGuard {
level: Rc<RefCell<u32>>,
}
impl LevelGuard {
fn new(owner: &LazifierVisitor) -> Self {
let result = Self {
level: owner.level.clone(),
};
*result.level.borrow_mut() += 1;
result
}
}
impl Drop for LevelGuard {
fn drop(&mut self) {
*self.level.borrow_mut() -= 1;
}
}
impl WalkGuard<LazifierVisitor> for Option<LevelGuard> {
fn new(_: &LazifierVisitor, _: &WalkPath) -> Option<LevelGuard> {
None
}
}
pub struct LazifierVisitor {
threshold: u32,
level: Rc<RefCell<u32>>,
}
impl LazifierVisitor {
pub fn new(threshold: u32) -> Self {
Self {
threshold,
level: Rc::new(RefCell::new(0)),
}
}
pub fn annotate_script(&mut self, script: &mut Script) -> Result<(), EnrichError> {
script.walk(&mut WalkPath::new(), self)?;
Ok(())
}
}
impl LazifierVisitor {
fn steal<T: Default, F, U>(source: &mut T, decorator: F) -> ExitResult<U>
where
F: FnOnce(T) -> U,
{
let mut stolen = T::default();
std::mem::swap(source, &mut stolen);
Ok(Some(decorator(stolen)))
}
fn cut_at_threshold(&mut self) -> EnterResult {
{
if *self.level.borrow() >= self.threshold {
return Ok(VisitMe::DoneHere);
}
}
Ok(VisitMe::HoldThis(Some(LevelGuard::new(self))))
}
}
impl Visitor<EnrichError, Option<LevelGuard>> for LazifierVisitor {
fn enter_method_definition(
&mut self,
_path: &WalkPath,
_node: &mut ViewMutMethodDefinition,
) -> EnterResult {
self.cut_at_threshold()
}
fn exit_method_definition(
&mut self,
_path: &WalkPath,
node: &mut ViewMutMethodDefinition,
) -> ExitResult<MethodDefinition> {
match *node {
ViewMutMethodDefinition::EagerGetter(ref mut steal) => Self::steal(*steal, |stolen| {
LazyGetter {
name: stolen.name,
directives: stolen.directives,
contents_skip: Offset::default(),
contents: stolen.contents,
}
.into()
}),
ViewMutMethodDefinition::EagerSetter(ref mut steal) => Self::steal(*steal, |stolen| {
LazySetter {
name: stolen.name,
length: stolen.length,
directives: stolen.directives,
contents_skip: Offset::default(),
contents: stolen.contents,
}
.into()
}),
ViewMutMethodDefinition::EagerMethod(ref mut steal) => Self::steal(*steal, |stolen| {
LazyMethod {
is_async: stolen.is_async,
is_generator: stolen.is_generator,
name: stolen.name,
length: stolen.length,
directives: stolen.directives,
contents_skip: Offset::default(),
contents: stolen.contents,
}
.into()
}),
_ => Ok(None),
}
}
fn enter_function_declaration(
&mut self,
_path: &WalkPath,
_node: &mut ViewMutFunctionDeclaration,
) -> EnterResult {
self.cut_at_threshold()
}
fn exit_function_declaration(
&mut self,
_path: &WalkPath,
node: &mut ViewMutFunctionDeclaration,
) -> ExitResult<FunctionDeclaration> {
match *node {
ViewMutFunctionDeclaration::EagerFunctionDeclaration(ref mut steal) => {
Self::steal(*steal, |stolen| {
LazyFunctionDeclaration {
is_async: stolen.is_async,
is_generator: stolen.is_generator,
name: stolen.name,
length: stolen.length,
directives: stolen.directives,
contents_skip: Offset::default(),
contents: stolen.contents,
}
.into()
})
}
_ => Ok(None),
}
}
fn enter_function_expression(
&mut self,
_path: &WalkPath,
_node: &mut ViewMutFunctionExpression,
) -> EnterResult {
self.cut_at_threshold()
}
fn exit_function_expression(
&mut self,
path: &WalkPath,
node: &mut ViewMutFunctionExpression,
) -> ExitResult<FunctionExpression> {
if let Some(WalkPathItem {
interface: ASTNode::CallExpression,
field: ASTField::Callee,
}) = path.get(0)
{
return Ok(None);
}
match *node {
ViewMutFunctionExpression::EagerFunctionExpression(ref mut steal) => {
Self::steal(*steal, |stolen| {
LazyFunctionExpression {
is_async: stolen.is_async,
is_generator: stolen.is_generator,
name: stolen.name,
length: stolen.length,
directives: stolen.directives,
contents_skip: Offset::default(),
contents: stolen.contents,
}
.into()
})
}
_ => Ok(None),
}
}
}