Skip to content

lua_getinfo and lua_getstack incorrect parameters #33

Open

Description

In the current implementation these are passed to lua as value while they should actually be ptrs for reading data out. One fix could be to replace the struct with an nint and just let .net deal string cancer like this

[DllImport(DllName, CallingConvention = Convention)]
public static extern int lua_getinfo(lua_State L, string what, nint ar);

    public static int lua_getinfo(lua_State L, string what, ref lua_Debug ar)
    {
        nint ptr = Marshal.AllocHGlobal(Marshal.SizeOf<lua_Debug>());
        int v;
        try
        {
            v = lua_getinfo(L, what, ptr);
            ar = Marshal.PtrToStructure<lua_Debug>(ptr);
        }
        finally
        {
            Marshal.FreeHGlobal(ptr);
        }
        return v;
    }

https://www.lua.org/manual/5.1/manual.html#lua_getinfo
https://www.lua.org/manual/5.1/manual.html#lua_getstack

Activity

  1. self-assigned this
    on Nov 23, 2025
  2. tilkinsc commented on Nov 23, 2025

    @tilkinsc
    Owner

    I propose it to look like this

    	[DllImport(DllName, CallingConvention = Convention)]
    	public static extern int lua_getstack(lua_State L, int level, ref lua_Debug ar);
    	
    	[DllImport(DllName, CallingConvention = Convention)]
    	public static extern int lua_getinfo(lua_State L, string what, ref lua_Debug ar);

    Since you can just use a ref here. No need to manually allocate it, since that is handled already.

    Do you have anything against doing it this way?

  3. tilkinsc commented on Nov 23, 2025

    @tilkinsc
    Owner

    Woopsie wrong one

  4. 1xKvSUbAg1xJx9KutZW1lzrdGImI3CaW commented on Nov 24, 2025

    @1xKvSUbAg1xJx9KutZW1lzrdGImI3CaW
    Author

    The only problem doing it this was is that the strings wont be marshelled properly, perhaps use libraryimport and have sourcegen deal with it?

  5. tilkinsc commented on Dec 9, 2025

    @tilkinsc
    Owner

    I've never had a problem with the strings marshalling as parameters. Only when they are returned from a function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions