moonseed 0.2.0

Deterministic, checkpointable Lua 5.4 runtime in safe Rust
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use std::cell::RefCell;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;

use crate::heap::Heap;
use crate::id::{ObjectId, OwnerToken};
use crate::{HostUserdata, LightUserdata, LuaType, Runtime};

use super::roots::{RootTable, Rooted};
use super::{ConversionError, FromLua, IntoLua, Result};

/// An owned Lua value. Object variants keep their targets alive (ADR 0053).
/// Clones share one root; the last drop releases it without borrowing the VM.
/// Roots can outlive the [`Runtime`] as Rust objects, but using them in another
/// runtime (including after restore) returns [`super::ApiError::WrongRuntime`].
/// [`ObjectId`] survives checkpoints without keeping anything alive. Use
/// [`ValueRef`] for a borrowed view and [`super::Coerce`] for explicit coercion.
///
/// ```
/// use moonseed::{Runtime, Value};
/// let mut rt = Runtime::builder().build()?;
/// let table = rt.create_table()?;
/// let kept = Value::Table(table.clone());
/// drop(table); // kept still roots the table
/// assert!(kept.id().is_some());
/// let restored = Runtime::restore(&rt.snapshot().unwrap(), &moonseed::Host::default())?;
/// assert!(matches!(kept.as_ref(&restored), Err(moonseed::Error::Api(moonseed::ApiError::WrongRuntime))));
/// # Ok::<(), moonseed::Error>(())
/// ```
#[derive(Clone, Debug)]
pub enum Value {
    /// Lua nil.
    Nil,
    /// Lua boolean.
    Boolean(bool),
    /// Lua integer.
    Integer(i64),
    /// Lua floating-point number.
    Number(f64),
    /// An opaque identity token.
    LightUserdata(LightUserdata),
    /// A rooted byte string.
    String(LuaString),
    /// A rooted table.
    Table(Table),
    /// A function of any supported kind.
    Function(Function),
    /// A rooted Lua thread.
    Thread(Thread),
    /// A rooted full userdata.
    UserData(AnyUserData),
}

macro_rules! object_type {
    ($name:ident, $doc:literal) => {
        #[doc = $doc]
        /// Clones share a root in one [`Runtime`]; the last drop releases it.
        /// Operations reject foreign runtime ownership. Restore preserves its
        /// logical [`ObjectId`], but roots must be reacquired in the new runtime.
        /// See [`Value`] for an ownership example and [`Runtime::snapshot`] for
        /// checkpoints. Borrowed string/payload views end before execution.
        #[derive(Clone)]
        pub struct $name(pub(crate) Rooted);

        impl $name {
            /// The logical identity, preserved across snapshots.
            pub fn id(&self) -> ObjectId {
                // These wrappers are only made for collectable objects.
                self.0.id.expect("collectable object identity")
            }
        }

        impl fmt::Debug for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_tuple(stringify!($name)).field(&self.0.id).finish()
            }
        }
    };
}

object_type!(LuaString, "A rooted Lua byte string.");
object_type!(Table, "A rooted Lua table. Raw operations never call Lua.");
object_type!(Thread, "A rooted Lua thread.");
object_type!(
    AnyUserData,
    "A rooted full userdata, with checked Rust payload borrows."
);

/// A Lua, native, native closure, or builtin function.
/// Clones root the same collectable closure in one [`Runtime`]. Bare natives
/// have a stable registered symbol and no [`ObjectId`]. Foreign roots fail with
/// [`super::ApiError::WrongRuntime`]. Use [`Runtime::call`] outside callbacks and
/// [`super::NativeContext::call_lua`] inside them. Closures/captures are snapshot
/// state; callback code must be re-registered through [`crate::HostRegistry`].
/// See the crate quick start and [`super::Host`] for execution/restore examples.
#[derive(Clone)]
pub struct Function(pub(crate) Rooted);

impl fmt::Debug for Function {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Function").field(&self.0.id).finish()
    }
}

/// A function's implementation kind, for introspection only.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FunctionKind {
    /// A compiled Lua closure.
    Lua,
    /// A host native function.
    Native,
    /// A collectable native closure.
    NativeClosure,
    /// A function implemented by the VM.
    Builtin,
}

/// A thread's drive state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ThreadStatus {
    /// Ready to execute, including a fuel pause.
    Ready,
    /// Suspended by Lua yield, or not started yet.
    Suspended,
    /// Waiting for a host operation.
    Waiting,
    /// Completed successfully.
    Completed,
    /// Ended with an uncaught Lua error.
    Failed,
}

impl Value {
    /// A collectable object's identity; primitives and bare natives have none.
    pub fn id(&self) -> Option<ObjectId> {
        match self {
            Self::String(value) => Some(value.id()),
            Self::Table(value) => Some(value.id()),
            Self::Function(value) => value.id(),
            Self::Thread(value) => Some(value.id()),
            Self::UserData(value) => Some(value.id()),
            _ => None,
        }
    }

    /// Lua's language-level type.
    pub fn lua_type(&self) -> LuaType {
        match self {
            Self::Nil => LuaType::Nil,
            Self::Boolean(_) => LuaType::Boolean,
            Self::Integer(_) | Self::Number(_) => LuaType::Number,
            Self::LightUserdata(_) | Self::UserData(_) => LuaType::Userdata,
            Self::String(_) => LuaType::String,
            Self::Table(_) => LuaType::Table,
            Self::Function(_) => LuaType::Function,
            Self::Thread(_) => LuaType::Thread,
        }
    }

    /// Borrow this value without adding another root.
    pub fn as_ref<'a>(&self, runtime: &'a Runtime) -> Result<ValueRef<'a>> {
        let value = self.raw(runtime)?;
        Ok(ValueRef::new(runtime, value))
    }

    pub(crate) fn check(&self, runtime: &Runtime) -> Result<()> {
        self.raw(runtime).map(|_| ())
    }

    #[inline]
    pub(crate) fn raw(&self, runtime: &Runtime) -> Result<crate::value::Value> {
        use crate::value::Value as Raw;
        Ok(match self {
            Self::Nil => Raw::Nil,
            Self::Boolean(value) => Raw::Bool(*value),
            Self::Integer(value) => Raw::Integer(*value),
            Self::Number(value) => Raw::Float(*value),
            Self::LightUserdata(value) => Raw::LightUserdata(value.domain, value.bits),
            Self::String(value) => value.0.value(runtime.owner())?,
            Self::Table(value) => value.0.value(runtime.owner())?,
            Self::Function(value) => value.0.value(runtime.owner())?,
            Self::Thread(value) => value.0.value(runtime.owner())?,
            Self::UserData(value) => value.0.value(runtime.owner())?,
        })
    }

    #[inline]
    pub(crate) fn wrap(
        value: crate::value::Value,
        heap: &Heap,
        owner: OwnerToken,
        roots: Option<&Rc<RefCell<RootTable>>>,
    ) -> Result<Self> {
        use crate::value::Value as Raw;
        let root = || -> Result<Rooted> {
            let roots = roots.ok_or(super::ApiError::Released)?;
            let id = heap.object_id_of_value(value);
            if !matches!(value, Raw::Native(_)) && id.is_none() {
                return Err(super::ApiError::Released.into());
            }
            Ok(Rooted::new(roots, owner, id, value))
        };
        Ok(match value {
            Raw::Nil => Self::Nil,
            Raw::Bool(value) => Self::Boolean(value),
            Raw::Integer(value) => Self::Integer(value),
            Raw::Float(value) => Self::Number(value),
            Raw::LightUserdata(domain, bits) => Self::LightUserdata(LightUserdata { domain, bits }),
            Raw::String(_) => Self::String(LuaString(root()?)),
            Raw::Table(_) => Self::Table(Table(root()?)),
            Raw::Closure(_) | Raw::Native(_) | Raw::NativeClosure(_) => {
                Self::Function(Function(root()?))
            }
            Raw::Thread(_) => Self::Thread(Thread(root()?)),
            Raw::Userdata(_) => Self::UserData(AnyUserData(root()?)),
        })
    }
}

/// An ephemeral view tied to the runtime borrow. It adds no root.
///
/// ```compile_fail
/// use moonseed::{Runtime, ValueRef};
/// let view: ValueRef<'_>;
/// {
///     let mut runtime = Runtime::builder().build().unwrap();
///     let string = runtime.create_string(b"borrowed").unwrap();
///     view = runtime.object_ref(string.id()).unwrap();
/// }
/// let _ = view.lua_type();
/// ```
#[derive(Clone, Copy)]
pub struct ValueRef<'a> {
    pub(crate) value: crate::value::Value,
    heap: &'a Heap,
    owner: OwnerToken,
    roots: Option<&'a Rc<RefCell<RootTable>>>,
}

impl<'a> ValueRef<'a> {
    pub(crate) fn new(runtime: &'a Runtime, value: crate::value::Value) -> Self {
        Self {
            value,
            heap: runtime.heap(),
            owner: runtime.owner(),
            roots: runtime.heap().api_roots.as_ref(),
        }
    }

    /// Lua's language-level type.
    pub fn lua_type(&self) -> LuaType {
        use crate::value::Value as Raw;
        match self.value {
            Raw::Nil => LuaType::Nil,
            Raw::Bool(_) => LuaType::Boolean,
            Raw::Integer(_) | Raw::Float(_) => LuaType::Number,
            Raw::String(_) => LuaType::String,
            Raw::Table(_) => LuaType::Table,
            Raw::Closure(_) | Raw::Native(_) | Raw::NativeClosure(_) => LuaType::Function,
            Raw::Thread(_) => LuaType::Thread,
            Raw::Userdata(_) | Raw::LightUserdata(..) => LuaType::Userdata,
        }
    }

    /// The logical object identity, if collectable.
    pub fn id(&self) -> Option<ObjectId> {
        self.heap.object_id_of_value(self.value)
    }

    /// Root this view. The shared root table allows rooting during a borrow;
    /// no collection can advance while the view lives.
    pub fn to_owned_value(&self) -> Result<Value> {
        Value::wrap(self.value, self.heap, self.owner, self.roots)
    }

    /// Whether this value is nil.
    pub fn is_nil(&self) -> bool {
        matches!(self.value, crate::value::Value::Nil)
    }

    /// Read a light userdata token by value.
    pub fn as_light_userdata(&self) -> Option<LightUserdata> {
        if let crate::value::Value::LightUserdata(domain, bits) = self.value {
            Some(LightUserdata { domain, bits })
        } else {
            None
        }
    }

    /// Read an integer without coercion.
    pub fn as_integer(&self) -> Option<i64> {
        if let crate::value::Value::Integer(value) = self.value {
            Some(value)
        } else {
            None
        }
    }

    /// Read a number without string coercion.
    pub fn as_number(&self) -> Option<f64> {
        match self.value {
            crate::value::Value::Integer(value) => Some(value as f64),
            crate::value::Value::Float(value) => Some(value),
            _ => None,
        }
    }

    /// Read a boolean without truthiness coercion.
    pub fn as_boolean(&self) -> Option<bool> {
        if let crate::value::Value::Bool(value) = self.value {
            Some(value)
        } else {
            None
        }
    }

    /// Borrow a string's bytes without a root or allocation.
    pub fn as_string(&self) -> Option<StrRef<'a>> {
        if let crate::value::Value::String(handle) = self.value {
            self.heap.string_bytes(handle).map(StrRef)
        } else {
            None
        }
    }
}

impl fmt::Debug for ValueRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ValueRef")
            .field("type", &self.lua_type())
            .field("id", &self.id())
            .finish()
    }
}

/// A borrowed Lua byte string.
#[derive(Clone, Copy)]
pub struct StrRef<'a>(&'a [u8]);

impl<'a> StrRef<'a> {
    /// The exact bytes, including NUL and invalid UTF-8.
    pub fn as_bytes(&self) -> &'a [u8] {
        self.0
    }

    /// A strict UTF-8 view.
    pub fn to_str(&self) -> Result<&'a str> {
        std::str::from_utf8(self.0).map_err(|_| {
            ConversionError {
                expected: "UTF-8 string",
                actual: LuaType::String,
                position: None,
            }
            .into()
        })
    }
}

impl fmt::Debug for StrRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("StrRef").field(&self.0).finish()
    }
}

impl LuaString {
    /// Borrow the exact string bytes from its runtime.
    pub fn as_bytes<'a>(&self, runtime: &'a Runtime) -> Result<&'a [u8]> {
        runtime.api_string(self)
    }

    /// Borrow strict UTF-8, refusing invalid bytes.
    pub fn to_str<'a>(&self, runtime: &'a Runtime) -> Result<&'a str> {
        StrRef(self.as_bytes(runtime)?).to_str()
    }
}

impl Table {
    /// Read through `__index` on the idle main thread, driving at most
    /// `fuel` units. On wait or fuel exhaustion, continue with `run` and
    /// `finish_call`; starting another operation while it runs is `Busy`.
    pub fn get<K: IntoLua, R: super::FromLuaMulti>(
        &self,
        runtime: &mut Runtime,
        key: K,
        journal: &mut crate::Journal,
        fuel: u64,
    ) -> Result<super::CallOutcome<R>> {
        self.0.value(runtime.owner())?;
        let key = key.into_lua(runtime)?;
        runtime.api_table_get(self, key, journal, fuel)
    }

    /// Write through `__newindex`, with the same bounded driving and
    /// completion protocol as `get`.
    pub fn set<K: IntoLua, V: IntoLua>(
        &self,
        runtime: &mut Runtime,
        key: K,
        value: V,
        journal: &mut crate::Journal,
        fuel: u64,
    ) -> Result<super::CallOutcome<()>> {
        self.0.value(runtime.owner())?;
        let key = key.into_lua(runtime)?;
        let value = value.into_lua(runtime)?;
        runtime.api_table_set(self, key, value, journal, fuel)
    }

    /// Read without metamethods. Nil and NaN keys are refused.
    pub fn raw_get<K: IntoLua, V: FromLua>(&self, runtime: &mut Runtime, key: K) -> Result<V> {
        self.0.value(runtime.owner())?;
        let key = key.into_lua(runtime)?;
        let value = runtime.api_raw_get(self, &key)?;
        V::from_lua(value, runtime)
    }

    /// Write without metamethods. Nil deletes; nil and NaN keys are refused.
    pub fn raw_set<K: IntoLua, V: IntoLua>(
        &self,
        runtime: &mut Runtime,
        key: K,
        value: V,
    ) -> Result<()> {
        self.0.value(runtime.owner())?;
        let key = key.into_lua(runtime)?;
        let value = value.into_lua(runtime)?;
        runtime.api_raw_set(self, &key, &value)
    }

    /// The raw table border, without `__len`.
    pub fn raw_len(&self, runtime: &Runtime) -> Result<i64> {
        runtime.api_raw_len(self)
    }

    /// The next pair in raw traversal order. `None` starts traversal;
    /// nil, NaN, and invalid anchors are refused as `InvalidKey`. Uses the
    /// runtime's iteration profile; sorted traversal charges per stored slot
    /// and rejects identity-bearing keys with a Lua error.
    pub fn next(
        &self,
        runtime: &mut Runtime,
        key: Option<&Value>,
    ) -> Result<Option<(Value, Value)>> {
        runtime.api_next(self, key)
    }

    /// The actual metatable, bypassing `__metatable` protection.
    pub fn metatable(&self, runtime: &mut Runtime) -> Result<Option<Table>> {
        runtime.api_metatable(self)
    }

    /// Set or clear the actual metatable, bypassing protection.
    pub fn set_metatable(&self, runtime: &mut Runtime, table: Option<&Table>) -> Result<()> {
        runtime.api_set_metatable(self, table)
    }
}

impl Function {
    /// The identity of a collectable closure. Bare natives and builtins
    /// have no object identity; their binding still belongs to one runtime.
    pub fn id(&self) -> Option<ObjectId> {
        self.0.id
    }

    /// Inspect the implementation kind, checking runtime ownership.
    pub fn kind(&self, runtime: &Runtime) -> Result<FunctionKind> {
        runtime.api_function_kind(self)
    }
}

impl Thread {
    /// Inspect the thread's drive state, checking runtime ownership.
    pub fn status(&self, runtime: &Runtime) -> Result<ThreadStatus> {
        runtime.api_thread_status(self)
    }
}

impl AnyUserData {
    /// Borrow a payload of exactly the registered Rust type.
    pub fn borrow<'a, T: HostUserdata>(&self, runtime: &'a Runtime) -> Result<&'a T> {
        runtime.api_userdata(self)
    }

    /// Borrow a mutable payload. Dropping the guard updates its logical
    /// charge, including growth past the quota, as the existing host API does.
    pub fn borrow_mut<'a, T: HostUserdata>(
        &self,
        runtime: &'a mut Runtime,
    ) -> Result<UserDataRefMut<'a, T>> {
        runtime.api_userdata_mut(self)
    }
}

/// A mutable userdata payload borrow with automatic charge reconciliation.
pub struct UserDataRefMut<'a, T: HostUserdata> {
    pub(crate) runtime: &'a mut Runtime,
    // Validated before construction; the exclusive runtime borrow prevents
    // collection, slot reuse, or restore for the entire guard lifetime.
    pub(crate) handle: crate::id::Handle<crate::heap::UserdataObj>,
    pub(crate) marker: std::marker::PhantomData<T>,
}

impl<T: HostUserdata> Deref for UserDataRefMut<'_, T> {
    type Target = T;
    fn deref(&self) -> &T {
        self.runtime
            .api_userdata_at(self.handle)
            .expect("borrowed userdata type")
    }
}

impl<T: HostUserdata> DerefMut for UserDataRefMut<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.runtime
            .api_userdata_at_mut(self.handle)
            .expect("borrowed userdata type")
    }
}

impl<T: HostUserdata> Drop for UserDataRefMut<'_, T> {
    fn drop(&mut self) {
        self.runtime.api_userdata_recharge::<T>(self.handle);
    }
}