lua_bytecode/
constant.rs

1use crate::RawLuaString;
2
3#[cfg(feature = "lua51")]
4pub const LUA_CONSTANT_NIL: u8 = 0;
5#[cfg(feature = "lua51")]
6pub const LUA_CONSTANT_BOOLEAN: u8 = 1;
7#[cfg(feature = "lua51")]
8pub const LUA_CONSTANT_NUMBER: u8 = 3;
9#[cfg(feature = "lua51")]
10pub const LUA_CONSTANT_STRING: u8 = 4;
11
12#[cfg(feature = "luau")]
13pub const LUAU_CONSTANT_NIL: u8 = 0;
14#[cfg(feature = "luau")]
15pub const LUAU_CONSTANT_BOOLEAN: u8 = 1;
16#[cfg(feature = "luau")]
17pub const LUAU_CONSTANT_NUMBER: u8 = 2;
18#[cfg(feature = "luau")]
19pub const LUAU_CONSTANT_STRING: u8 = 3;
20#[cfg(feature = "luau")]
21pub const LUAU_CONSTANT_IMPORT: u8 = 4;
22#[cfg(feature = "luau")]
23pub const LUAU_CONSTANT_TABLE: u8 = 5;
24#[cfg(feature = "luau")]
25pub const LUAU_CONSTANT_CLOSURE: u8 = 6;
26#[cfg(feature = "luau")]
27pub const LUAU_CONSTANT_VECTOR: u8 = 7;
28
29#[derive(Clone, Debug, Default)]
30pub enum Constant {
31    #[default]
32    Nil,
33    Bool(bool),
34    Number(f64),
35    String(RawLuaString),
36
37    #[cfg(feature = "luau")]
38    Vector(f32, f32, f32, f32),
39    #[cfg(feature = "luau")]
40    Closure(u32),
41    #[cfg(feature = "luau")]
42    Import(i32),
43    #[cfg(feature = "luau")]
44    Table(u32, Vec<u32>),
45}
46
47// TODO: hacky code, maybe use traits?
48impl Constant {
49    #[cfg(feature = "lua51")]
50    pub fn kind(&self) -> u8 {
51        match self {
52            Constant::Nil => LUA_CONSTANT_NIL,
53            Constant::Bool(_) => LUA_CONSTANT_BOOLEAN,
54            Constant::Number(_) => LUA_CONSTANT_NUMBER,
55            Constant::String(_) => LUA_CONSTANT_STRING,
56
57            _ => unreachable!(),
58        }
59    }
60
61    #[cfg(feature = "luau")]
62    pub fn kind_luau(&self) -> u8 {
63        match self {
64            Constant::Nil => LUAU_CONSTANT_NIL,
65            Constant::Bool(_) => LUAU_CONSTANT_BOOLEAN,
66            Constant::Number(_) => LUAU_CONSTANT_NUMBER,
67            Constant::String(_) => LUAU_CONSTANT_STRING,
68            Constant::Vector(_, _, _, _) => LUAU_CONSTANT_VECTOR,
69            Constant::Closure(_) => LUAU_CONSTANT_CLOSURE,
70            Constant::Import(_) => LUAU_CONSTANT_IMPORT,
71            Constant::Table(_, _) => LUAU_CONSTANT_TABLE,
72        }
73    }
74}