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
use std::fmt::{Debug, Display};
use crate::code::{Code, FunctionKind};
use crate::gc::{Gc, RefCell, Trace};
use crate::utils::ValueVecDebug;
use super::Value;
#[derive(Clone)]
pub struct Closure {
pub function: Code,
pub base_closure: Option<Gc<RefCell<Closure>>>,
pub upvalues: Vec<Value>,
}
unsafe impl Trace for Closure {
#[inline]
unsafe fn trace(&self) {
for i in &self.upvalues {
i.trace();
}
}
}
impl Debug for Closure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Closure")
.field("function", &self.function)
.field(
"base_closure",
&self.base_closure.as_ref().map(|v| v.borrow()),
)
.field("variables", &ValueVecDebug(&self.upvalues))
.finish()
}
}
impl PartialEq for Closure {
fn eq(&self, _: &Self) -> bool {
false
}
}
impl Eq for Closure {}
impl Display for Closure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.function.kind {
FunctionKind::Funciton => write!(f, "<function>"),
FunctionKind::Closure => write!(f, "<function(closure)>"),
FunctionKind::Do => write!(f, "<function(do)>"),
}
}
}
impl Closure {
pub fn new(function: Code, base_closure: Option<Gc<RefCell<Closure>>>) -> Self {
Closure {
base_closure,
upvalues: vec![Value::Null; function.def_upvalue_count],
function,
}
}
}