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
//! Private tagged values. Handles, never Rust references.
use crate::heap::{ClosureObj, NativeClosureObj, StringObj, TableObj, ThreadObj, UserdataObj};
use crate::id::Handle;
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) enum Value {
Nil,
Bool(bool),
Integer(i64),
Float(f64),
String(Handle<StringObj>),
Table(Handle<TableObj>),
Closure(Handle<ClosureObj>),
Thread(Handle<ThreadObj>),
/// A registered native function: an index into the runtime's interned
/// native symbols (`Heap::natives`). Same symbol, same index. Not a
/// collectable object.
Native(u32),
/// A function the VM implements that keeps values and state of its
/// own, such as `string.gmatch`'s iterator (ADR 0035).
NativeClosure(Handle<NativeClosureObj>),
/// A full userdata (ADR 0042): an object with its own metatable, a
/// fixed number of user values, and a payload Lua cannot read.
Userdata(Handle<UserdataObj>),
/// A light userdata (ADR 0043): an identity token, never an address
/// and never an object. Equal when domain and bits are.
LightUserdata(LightDomain, u64),
}
/// Who made a light userdata's token (ADR 0043). The domains keep a host
/// key from ever equalling a token the VM makes.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u8)]
pub(crate) enum LightDomain {
/// A host key, [`crate::HostLightKey`]: any `u64` the host chooses.
Host = 0,
/// `debug.upvalueid` of a Lua closure: the upvalue cell's `ObjectId`.
Upvalue = 1,
/// `debug.upvalueid` of a native closure: the closure's `ObjectId`
/// shifted left 8 bits, plus the value's index.
NativeValue = 2,
}
impl LightDomain {
pub(crate) fn from_tag(tag: u8) -> Option<Self> {
Some(match tag {
0 => Self::Host,
1 => Self::Upvalue,
2 => Self::NativeValue,
_ => return None,
})
}
}
// Two new value kinds must not make every value bigger.
const _: () = assert!(std::mem::size_of::<Value>() == 16);
impl Value {
/// Whether Lua's type of the value is `function`.
#[inline(always)]
pub(crate) fn is_function(self) -> bool {
matches!(
self,
Value::Closure(_) | Value::Native(_) | Value::NativeClosure(_)
)
}
/// Lua truth: `nil` and `false` are false, every other value is true,
/// `0`, `-0.0`, and `""` included. The one definition for conditions,
/// `not`, `and`, `or`, a comparison metamethod's result, and which
/// values `<close>` ignores.
#[inline(always)]
pub(crate) fn truthy(self) -> bool {
!matches!(self, Value::Nil | Value::Bool(false))
}
}
#[cfg(test)]
mod tests {
use super::Value;
#[test]
fn only_nil_and_false_are_false() {
assert!(!Value::Nil.truthy());
assert!(!Value::Bool(false).truthy());
for value in [
Value::Bool(true),
Value::Integer(0),
Value::Float(0.0),
Value::Float(-0.0),
Value::Float(f64::NAN),
Value::Native(0),
// Any string, "" included, and any table or function.
Value::String(crate::id::Handle::new(0, 0)),
Value::Table(crate::id::Handle::new(0, 0)),
Value::Closure(crate::id::Handle::new(0, 0)),
] {
assert!(value.truthy(), "{value:?}");
}
}
}