Skip to main content

luaur_code_gen/functions/
new_userdata.rs

1use core::ffi::c_int;
2
3use luaur_common::macros::luau_assert::LUAU_ASSERT;
4use luaur_vm::macros::isblack::isblack;
5use luaur_vm::records::gc_object::GCObject;
6use luaur_vm::records::lua_state::lua_State;
7use luaur_vm::records::udata::Udata;
8
9unsafe extern "C" {
10    #[link_name = "luaU_newudata"]
11    fn luaU_newudata(L: *mut lua_State, s: usize, tag: c_int) -> *mut Udata;
12}
13
14#[allow(non_snake_case)]
15pub unsafe fn new_userdata(L: *mut lua_State, s: usize, tag: i32) -> *mut Udata {
16    let u = luaU_newudata(L, s, tag);
17
18    let h = (*(*L).global).udatamt[tag as usize];
19    if !h.is_null() {
20        // currently, we always allocate unmarked objects, so forward barrier can be skipped
21        LUAU_ASSERT!(!isblack!(u as *mut GCObject));
22
23        (*u).metatable = h;
24    }
25
26    u
27}
28
29#[no_mangle]
30pub unsafe extern "C" fn newUserdata(
31    L: *mut lua_State,
32    s: usize,
33    tag: c_int,
34) -> *mut core::ffi::c_void {
35    new_userdata(L, s, tag).cast()
36}