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
use core::cell::Cell;
use crate::codegen::{FunctionID, FunctionProto};
use crate::runtime::Variant;
use crate::runtime::module::{Module, GlobalEnv};
use crate::runtime::vm::VirtualMachine;
use crate::runtime::gc::{Gc, GcTrace};
use crate::runtime::errors::ExecResult;
mod signature;
pub use signature::{Signature, Parameter};
pub use crate::codegen::opcodes::UpvalueIndex;
pub enum Call {
Chunk(Gc<Module>, FunctionID),
Native(Gc<NativeFunction>),
}
pub trait Callable {
fn signature(&self) -> &Signature;
fn raw_call(&self, args: &[Variant]) -> Call;
fn checked_call(&self, args: &[Variant]) -> ExecResult<Call> {
self.signature().check_args(args)?;
Ok(self.raw_call(args))
}
}
#[derive(Debug)]
pub struct Function {
fun_id: FunctionID,
module: Gc<Module>,
upvalues: Box<[Upvalue]>,
}
impl Function {
pub fn new(fun_id: FunctionID, module: Gc<Module>, upvalues: Box<[Upvalue]>) -> Self {
Self { fun_id, module, upvalues }
}
pub fn upvalues(&self) -> &[Upvalue] { &self.upvalues }
pub fn proto(&self) -> &FunctionProto {
self.module.data().get_function(self.fun_id)
}
pub fn signature(&self) -> &Signature {
self.proto().signature()
}
}
impl Callable for Function {
fn signature(&self) -> &Signature { self.proto().signature() }
fn raw_call(&self, _args: &[Variant]) -> Call {
Call::Chunk(self.module, self.fun_id)
}
}
impl Callable for Gc<Function> {
fn signature(&self) -> &Signature {
<Function as Callable>::signature(self)
}
fn raw_call(&self, args: &[Variant]) -> Call {
<Function as Callable>::raw_call(self, args)
}
}
unsafe impl GcTrace for Function {
fn trace(&self) {
self.module.mark_trace();
for upval in self.upvalues.iter() {
if let Closure::Closed(gc_cell) = upval.closure() {
gc_cell.mark_trace();
}
}
}
fn size_hint(&self) -> usize {
core::mem::size_of::<Upvalue>() * self.upvalues.len()
}
}
#[derive(Debug, Clone, Copy)]
pub enum Closure {
Open(usize),
Closed(Gc<Cell<Variant>>),
}
impl Closure {
pub fn is_open(&self) -> bool { matches!(self, Self::Open(..)) }
pub fn is_closed(&self) -> bool { matches!(self, Self::Closed(..)) }
}
unsafe impl GcTrace for Cell<Variant> {
fn trace(&self) {
self.get().trace()
}
}
#[derive(Debug, Clone)]
pub struct Upvalue {
value: Cell<Closure>,
}
impl Upvalue {
pub fn new(index: usize) -> Self {
Self {
value: Cell::new(Closure::Open(index)),
}
}
#[inline]
pub fn closure(&self) -> Closure { self.value.get() }
#[inline]
pub fn close(&self, gc_cell: Gc<Cell<Variant>>) {
self.value.set(Closure::Closed(gc_cell))
}
}
pub type NativeFn = fn(self_fun: &NativeFunction, vm: &mut VirtualMachine<'_>, args: &[Variant]) -> ExecResult<Variant>;
pub struct NativeFunction {
signature: Signature,
defaults: Option<Box<[Variant]>>,
env: Gc<GlobalEnv>,
func: NativeFn,
}
impl NativeFunction {
pub fn new(signature: Signature, defaults: Option<Box<[Variant]>>, env: Gc<GlobalEnv>, func: NativeFn) -> Self {
Self { signature, defaults, env, func }
}
pub fn signature(&self) -> &Signature { &self.signature }
pub fn env(&self) -> Gc<GlobalEnv> { self.env }
pub fn defaults(&self) -> &[Variant] {
match self.defaults.as_ref() {
Some(defaults) => &*defaults,
None => &[],
}
}
pub fn exec_fun(&self, vm: &mut VirtualMachine<'_>, args: &[Variant]) -> ExecResult<Variant> {
self.signature().check_args(args)?;
(self.func)(self, vm, args)
}
}
impl Callable for Gc<NativeFunction> {
fn signature(&self) -> &Signature { &self.signature }
fn raw_call(&self, _args: &[Variant]) -> Call {
Call::Native(*self)
}
}
unsafe impl GcTrace for NativeFunction {
fn trace(&self) {
Gc::mark_trace(&self.env);
if let Some(defaults) = self.defaults.as_ref() {
defaults.trace()
}
}
fn size_hint(&self) -> usize {
self.defaults.as_ref()
.map_or(0, |defaults| {
core::mem::size_of::<Variant>() * defaults.len()
})
}
}