Skip to content

Expose tracebacks in safe API #652

@bobbens

Description

@bobbens

I use tracebacks when displaying warnings. However, to the best of my knowledge, luaL_traceback is not exposed in the "safe" API. In order to get backtraces, I had to wrap the unsafe luaL_traceback with create_c_function to be able to get the backtraces. Providing some sort of lua.traceback() or the likes that would do this could be useful instead of needing a custom implementation.

For reference, my unsafe implementation is below:

        #[allow(non_snake_case)]
        unsafe extern "C-unwind" fn traceback_wrap(L: *mut mlua::lua_State) -> i32 {
            unsafe {
                let msg = if mlua::ffi::lua_isstring(L, 1) != 0 {
                    mlua::ffi::lua_tostring(L, 1)
                } else {
                    std::ptr::null()
                };
                let level = if mlua::ffi::lua_isinteger(L, 2) != 0 {
                    mlua::ffi::lua_tointeger(L, 2)
                } else {
                    1
                };
                mlua::ffi::luaL_traceback(L, L, msg, level as i32);
            }
            1
        }
        let traceback = unsafe { lua.create_c_function(traceback_wrap)? };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions