-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
context.rs
256 lines (221 loc) · 6.93 KB
/
context.rs
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use std::collections::{BTreeMap, HashSet};
use std::fmt;
use std::sync::Arc;
use crate::environment::Environment;
use crate::error::{Error, ErrorKind};
use crate::value::{OwnedValueIterator, Value};
use crate::vm::loop_object::Loop;
type Locals<'env> = BTreeMap<&'env str, Value>;
/// The maximum recursion in the VM. Normally each stack frame
/// adds one to this counter (eg: every time a frame is added).
/// However in some situations more depth is pushed if the cost
/// of the stack frame is higher.
const MAX_RECURSION: usize = 500;
pub(crate) struct LoopState {
pub(crate) with_loop_var: bool,
pub(crate) recurse_jump_target: Option<usize>,
// if we're popping the frame, do we want to jump somewhere? The
// first item is the target jump instruction, the second argument
// tells us if we need to end capturing.
pub(crate) current_recursion_jump: Option<(usize, bool)>,
pub(crate) iterator: OwnedValueIterator,
pub(crate) object: Arc<Loop>,
}
pub(crate) struct Frame<'env> {
pub(crate) locals: Locals<'env>,
pub(crate) ctx: Value,
pub(crate) current_loop: Option<LoopState>,
}
impl<'env> Default for Frame<'env> {
fn default() -> Frame<'env> {
Frame::new(Value::UNDEFINED)
}
}
impl<'env> Frame<'env> {
pub fn new(ctx: Value) -> Frame<'env> {
Frame {
locals: Locals::new(),
ctx,
current_loop: None,
}
}
}
#[cfg(feature = "internal_debug")]
impl<'env> fmt::Debug for Frame<'env> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut m = f.debug_map();
m.entries(self.locals.iter());
if let Some(LoopState {
object: ref controller,
..
}) = self.current_loop
{
m.entry(&"loop", controller);
}
if !self.ctx.is_undefined() {
m.entries(self.ctx.iter_as_str_map());
}
m.finish()
}
}
#[cfg_attr(feature = "internal_debug", derive(Debug))]
#[derive(Default)]
pub(crate) struct Stack {
values: Vec<Value>,
}
impl Stack {
pub fn push(&mut self, arg: Value) {
self.values.push(arg);
}
#[track_caller]
pub fn pop(&mut self) -> Value {
self.values.pop().unwrap()
}
pub fn slice_top(&mut self, n: usize) -> &[Value] {
&self.values[self.values.len() - n..]
}
pub fn drop_top(&mut self, n: usize) {
self.values.truncate(self.values.len() - n);
}
pub fn try_pop(&mut self) -> Option<Value> {
self.values.pop()
}
#[track_caller]
pub fn peek(&self) -> &Value {
self.values.last().unwrap()
}
}
impl From<Vec<Value>> for Stack {
fn from(values: Vec<Value>) -> Stack {
Stack { values }
}
}
#[derive(Default)]
pub(crate) struct Context<'env> {
stack: Vec<Frame<'env>>,
outer_stack_depth: usize,
}
impl<'env> fmt::Debug for Context<'env> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn dump<'a>(
m: &mut std::fmt::DebugMap,
seen: &mut HashSet<&'a str>,
ctx: &'a Context<'a>,
) -> fmt::Result {
for frame in ctx.stack.iter().rev() {
for (key, value) in frame.locals.iter() {
if !seen.contains(key) {
seen.insert(*key);
m.entry(key, value);
}
}
if let Some(ref l) = frame.current_loop {
if l.with_loop_var && !seen.contains(&"loop") {
seen.insert("loop");
m.entry(&"loop", &l.object);
}
}
for (key, value) in frame.ctx.iter_as_str_map() {
if !seen.contains(key) {
seen.insert(key);
m.entry(&key, &value);
}
}
}
Ok(())
}
let mut m = f.debug_map();
let mut seen = HashSet::new();
ok!(dump(&mut m, &mut seen, self));
m.finish()
}
}
impl<'env> Context<'env> {
/// Creates a context
pub fn new(frame: Frame<'env>) -> Context<'env> {
Context {
stack: vec![frame],
outer_stack_depth: 0,
}
}
/// Stores a variable in the context.
pub fn store(&mut self, key: &'env str, value: Value) {
self.stack.last_mut().unwrap().locals.insert(key, value);
}
/// Looks up a variable in the context.
pub fn load(&self, env: &Environment, key: &str) -> Option<Value> {
for frame in self.stack.iter().rev() {
// look at locals first
if let Some(value) = frame.locals.get(key) {
if !value.is_undefined() {
return Some(value.clone());
}
}
// if we are a loop, check if we are looking up the special loop var.
if let Some(ref l) = frame.current_loop {
if l.with_loop_var && key == "loop" {
return Some(Value::from(l.object.clone()));
}
}
// if the frame context is undefined, we skip the lookup
if !frame.ctx.is_undefined() {
if let Ok(rv) = frame.ctx.get_attr(key) {
if !rv.is_undefined() {
return Some(rv);
}
}
}
}
env.get_global(key)
}
/// Pushes a new layer.
pub fn push_frame(&mut self, layer: Frame<'env>) -> Result<(), Error> {
self.check_depth()?;
self.stack.push(layer);
Ok(())
}
/// Pops the topmost layer.
#[track_caller]
pub fn pop_frame(&mut self) -> Frame {
self.stack.pop().unwrap()
}
/// Returns the current locals.
#[track_caller]
#[cfg(feature = "multi-template")]
pub fn current_locals(&mut self) -> &mut Locals<'env> {
&mut self.stack.last_mut().unwrap().locals
}
/// Returns the current innermost loop.
pub fn current_loop(&mut self) -> Option<&mut LoopState> {
self.stack
.iter_mut()
.rev()
.filter_map(|x| x.current_loop.as_mut())
.next()
}
/// The real depth of the context.
pub fn depth(&self) -> usize {
self.outer_stack_depth + self.stack.len()
}
/// Increase the stack depth.
#[allow(unused)]
pub fn incr_depth(&mut self, delta: usize) -> Result<(), Error> {
self.check_depth()?;
self.outer_stack_depth += delta;
Ok(())
}
/// Decrease the stack depth.
#[allow(unused)]
pub fn decr_depth(&mut self, delta: usize) {
self.outer_stack_depth -= delta;
}
fn check_depth(&self) -> Result<(), Error> {
if self.depth() > MAX_RECURSION {
return Err(Error::new(
ErrorKind::InvalidOperation,
"recursion limit exceeded",
));
}
Ok(())
}
}