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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use std::ops;
use gc_arena::{metrics::Metrics, Arena, Collect, CollectionPhase, Mutation, Root, Rootable};
use crate::{
finalizers::Finalizers,
registry::{Fetchable, Stashable},
stdlib::{load_base, load_coroutine, load_io, load_math, load_string, load_table},
string::InternedStringSet,
Error, FromMultiValue, Fuel, IntoValue, InvalidTableKey, Registry, Singleton, StashedExecutor,
StaticError, String, Table, Value,
};
#[derive(Copy, Clone)]
pub struct Context<'gc> {
mutation: &'gc Mutation<'gc>,
state: &'gc State<'gc>,
}
impl<'gc> Context<'gc> {
pub fn globals(self) -> Table<'gc> {
self.state.globals
}
pub fn registry(self) -> Registry<'gc> {
self.state.registry
}
pub fn interned_strings(self) -> InternedStringSet<'gc> {
self.state.strings
}
pub fn finalizers(self) -> Finalizers<'gc> {
self.state.finalizers
}
/// Calls `ctx.globals().set(ctx, key, value)`.
pub fn set_global<K: IntoValue<'gc>, V: IntoValue<'gc>>(
self,
key: K,
value: V,
) -> Result<Value<'gc>, InvalidTableKey> {
self.state.globals.set(self, key, value)
}
/// Calls `ctx.globals().get(ctx, key)`.
pub fn get_global<K: IntoValue<'gc>>(self, key: K) -> Value<'gc> {
self.state.globals.get(self, key)
}
/// Calls `ctx.registry().singleton::<S>(ctx)`.
pub fn singleton<S>(self) -> &'gc Root<'gc, S>
where
S: for<'a> Rootable<'a>,
Root<'gc, S>: Singleton<'gc>,
{
self.state.registry.singleton::<S>(self)
}
/// Calls `ctx.registry().stash(ctx, s)`.
pub fn stash<S: Stashable<'gc>>(self, s: S) -> S::Stashed {
self.state.registry.stash(&self, s)
}
/// Calls `ctx.registry().fetch(f)`.
pub fn fetch<F: Fetchable<'gc>>(self, f: &F) -> F::Fetched {
self.state.registry.fetch(f)
}
/// Calls `ctx.interned_strings().intern(&ctx, s)`.
pub fn intern(self, s: &[u8]) -> String<'gc> {
self.state.strings.intern(&self, s)
}
/// Calls `ctx.interned_strings().intern_static(&ctx, s)`.
pub fn intern_static(self, s: &'static [u8]) -> String<'gc> {
self.state.strings.intern_static(&self, s)
}
}
impl<'gc> ops::Deref for Context<'gc> {
type Target = Mutation<'gc>;
fn deref(&self) -> &Self::Target {
self.mutation
}
}
pub struct Lua {
arena: Arena<Rootable![State<'_>]>,
}
impl Default for Lua {
fn default() -> Self {
Lua::core()
}
}
impl Lua {
/// Create a new `Lua` instance with no parts of the stdlib loaded.
pub fn empty() -> Self {
Lua {
arena: Arena::<Rootable![State<'_>]>::new(|mc| State::new(mc)),
}
}
/// Create a new `Lua` instance with the core stdlib loaded.
pub fn core() -> Self {
let mut lua = Self::empty();
lua.load_core();
lua
}
/// Create a new `Lua` instance with all of the stdlib loaded.
pub fn full() -> Self {
let mut lua = Lua::core();
lua.load_io();
lua
}
/// Load the core parts of the stdlib that do not allow performing any I/O.
///
/// Calls:
/// - `load_base`
/// - `load_coroutine`
/// - `load_math`
/// - `load_string`
/// - `load_table`
pub fn load_core(&mut self) {
self.enter(|ctx| {
load_base(ctx);
load_coroutine(ctx);
load_math(ctx);
load_string(ctx);
load_table(ctx);
})
}
/// Load the parts of the stdlib that allow I/O.
pub fn load_io(&mut self) {
self.enter(|ctx| {
load_io(ctx);
})
}
/// Size of all memory used by this Lua context.
///
/// This is equivalent to `self.gc_metrics().total_allocation()`. This counts all `Gc` allocated
/// memory and also all data Lua datastructures held inside `Gc`, as they are tracked as
/// "external allocations" in `gc-arena`.
pub fn total_memory(&self) -> usize {
self.gc_metrics().total_allocation()
}
/// Finish the current collection cycle completely, calls `gc_arena::Arena::collect_all()`.
pub fn gc_collect(&mut self) {
if self.arena.collection_phase() != CollectionPhase::Collecting {
self.arena.mark_all().unwrap().finalize(|fc, root| {
root.finalizers.prepare(fc);
});
self.arena.mark_all().unwrap().finalize(|fc, root| {
root.finalizers.finalize(fc);
});
}
self.arena.collect_all();
assert!(self.arena.collection_phase() == CollectionPhase::Sleeping);
}
pub fn gc_metrics(&self) -> &Metrics {
self.arena.metrics()
}
/// Enter the garbage collection arena and perform some operation.
///
/// In order to interact with Lua or do any useful work with Lua values, you must do so from
/// *within* the garbage collection arena. All values branded with the `'gc` branding lifetime
/// must forever live *inside* the arena, and cannot escape it.
///
/// Garbage collection takes place *in-between* calls to `Lua::enter`, no garbage will be
/// collected cocurrently with accessing the arena.
///
/// Automatically triggers garbage collection before returning if the allocation debt is larger
/// than a small constant.
pub fn enter<F, T>(&mut self, f: F) -> T
where
F: for<'gc> FnOnce(Context<'gc>) -> T,
{
const COLLECTOR_GRANULARITY: f64 = 1024.0;
let r = self.arena.mutate(move |mc, state| f(state.ctx(mc)));
if self.arena.metrics().allocation_debt() > COLLECTOR_GRANULARITY {
if self.arena.collection_phase() == CollectionPhase::Collecting {
self.arena.collect_debt();
} else {
if let Some(marked) = self.arena.mark_debt() {
marked.finalize(|fc, root| {
root.finalizers.prepare(fc);
});
self.arena.mark_all().unwrap().finalize(|fc, root| {
root.finalizers.finalize(fc);
});
// Immediately transition to `CollectionPhase::Collecting`.
self.arena.mark_all().unwrap().start_collecting();
}
}
}
r
}
/// A version of `Lua::enter` that expects failure and also automatically converts `Error` types
/// into `StaticError`, allowing the error type to escape the arena.
pub fn try_enter<F, R>(&mut self, f: F) -> Result<R, StaticError>
where
F: for<'gc> FnOnce(Context<'gc>) -> Result<R, Error<'gc>>,
{
self.enter(move |ctx| f(ctx).map_err(Error::into_static))
}
/// Run the given executor to completion.
///
/// This will periodically exit the arena in order to collect garbage concurrently with running
/// Lua code.
pub fn finish(&mut self, executor: &StashedExecutor) {
const FUEL_PER_GC: i32 = 4096;
loop {
let mut fuel = Fuel::with(FUEL_PER_GC);
if self.enter(|ctx| ctx.fetch(executor).step(ctx, &mut fuel)) {
break;
}
}
}
/// Run the given executor to completion and then take return values from the returning thread.
///
/// This is equivalent to calling `Lua::finish` on an executor and then calling
/// `Executor::take_result` yourself.
pub fn execute<R: for<'gc> FromMultiValue<'gc>>(
&mut self,
executor: &StashedExecutor,
) -> Result<R, StaticError> {
self.finish(executor);
self.try_enter(|ctx| ctx.fetch(executor).take_result::<R>(ctx)?)
}
}
#[derive(Copy, Clone, Collect)]
#[collect(no_drop)]
struct State<'gc> {
globals: Table<'gc>,
registry: Registry<'gc>,
strings: InternedStringSet<'gc>,
finalizers: Finalizers<'gc>,
}
impl<'gc> State<'gc> {
fn new(mc: &Mutation<'gc>) -> State<'gc> {
Self {
globals: Table::new(mc),
registry: Registry::new(mc),
strings: InternedStringSet::new(mc),
finalizers: Finalizers::new(mc),
}
}
fn ctx(&'gc self, mutation: &'gc Mutation<'gc>) -> Context<'gc> {
Context {
mutation,
state: self,
}
}
}