luaur_compiler/methods/
compiler_get_constant_index.rs1use crate::enums::type_constant_folding::Type;
2use crate::functions::sref_compiler_alt_c::sref_ast_array_c_char;
3use crate::records::compile_error::CompileError;
4use crate::records::compiler::Compiler;
5use crate::records::constant::Constant;
6use luaur_ast::records::ast_expr::AstExpr;
7use luaur_ast::records::location::Location;
8use luaur_common::macros::luau_assert::LUAU_ASSERT;
9
10impl Compiler {
11 pub fn get_constant_index(&mut self, node: *mut AstExpr) -> i32 {
12 unsafe {
13 let c = self.constants.find(&node);
14 if c.is_none() || (*c.unwrap()).r#type == Type::Type_Unknown {
15 return -1;
16 }
17
18 let constant = c.unwrap();
19 let mut cid = -1;
20
21 match (*constant).r#type {
22 Type::Type_Nil => {
23 cid = (*self.bytecode).add_constant_nil();
24 }
25 Type::Type_Boolean => {
26 cid = (*self.bytecode).add_constant_boolean((*constant).data.value_boolean);
27 }
28 Type::Type_Number => {
29 cid = (*self.bytecode).add_constant_number((*constant).data.value_number);
30 }
31 Type::Type_Integer => {
32 cid = (*self.bytecode).add_constant_integer((*constant).data.value_integer64);
33 }
34 Type::Type_Vector => {
35 cid = (*self.bytecode).add_constant_vector(
36 (*constant).data.value_vector[0],
37 (*constant).data.value_vector[1],
38 (*constant).data.value_vector[2],
39 (*constant).data.value_vector[3],
40 );
41 }
42 Type::Type_String => {
43 let string_data = (*constant).get_string();
44 cid = (*self.bytecode).add_constant_string(sref_ast_array_c_char(string_data));
45 }
46 _ => {
47 LUAU_ASSERT!(false);
48 return -1;
49 }
50 }
51
52 if cid < 0 {
53 CompileError::raise(
54 &(*node).base.location,
55 core::format_args!("Exceeded constant limit; simplify the code to compile"),
56 );
57 }
58
59 cid
60 }
61 }
62}