Skip to main content

luaur_compiler/functions/
sref_compiler.rs

1use luaur_ast::records::ast_name::AstName;
2use luaur_bytecode::records::string_ref::StringRef;
3use luaur_common::macros::luau_assert::LUAU_ASSERT;
4
5pub fn sref_ast_name(name: AstName) -> StringRef {
6    LUAU_ASSERT!(!name.value.is_null());
7
8    let len = unsafe { core::ffi::CStr::from_ptr(name.value).to_bytes().len() };
9
10    // StringRef fields are pub(crate) in luaur_bytecode. Since this is luaur_compiler,
11    // we cannot use struct literal syntax. We must use a constructor or transmute.
12    // Based on the StringRef API card, it derives Default, but has no public constructor.
13    // However, in this codebase, StringRef is a simple repr(C) pair.
14    #[repr(C)]
15    struct StringRefLayout {
16        data: *const core::ffi::c_char,
17        length: usize,
18    }
19
20    let layout = StringRefLayout {
21        data: name.value,
22        length: len,
23    };
24
25    unsafe { core::mem::transmute::<StringRefLayout, StringRef>(layout) }
26}