Closed UltraEngine closed 1 year ago
This is what the metatable looks like. I tried "UltraEngine::Window::BindClass::LuaWindowHandle" and "UltraEngine::Window::BindClass::LuaWindowHandle*" for the name, to no avail. https://imgur.com/a/qelNT1w
Let me try to confirm my understanding. In the example code you try to cast the return value of GetHandle to an arbitrary struct pointer to make the code work on all platforms. However, the code does not work.
The problem seems to be at least the way you cast from userdata into HWND. Also the metatable name may or may not be correct. The pointer returned by checkudata function is not the pointer you return from GetHandle. It is a pointer to the userdata that contains the pointer. So you should be using something on the lines of
LuaWindowHandle** udataptr = (LuaWindowHandle**)luaL_checkudata(L, 1, "sol.LuaWindowHandle*");
LuaWindowHandle* udata = *udataptr;
HWND hwnd = (HWND)udata;
Here is a small code I whipped up to try show how it could probably work. https://godbolt.org/z/3YhvPr3rn
You may also be interested in lightuserdata https://sol2.readthedocs.io/en/latest/api/user.html?highlight=light https://www.lua.org/pil/28.5.html
They are just void*
pointers that you can pass around through lua. But they provide no typesafety or metatables and thus no methods either.
Seems void pointer is automatically handled as lightuserdata by sol. So you can just cast to it and it should work. Here is an example code with the lightuserdata https://godbolt.org/z/s7fzKPMxM
Since you are making a module, which I guess is a DLL or SO or similar, take a look at this: integrating into existing code https://sol2.readthedocs.io/en/latest/tutorial/existing.html
It did not occur to me that sol might actually be able to handle void*. I am using this now and it works great.
I don't expect I will actually need to expose any C++ classes to a Lua module. I can't think of any reason it would be necessary.
Thanks for your help.
I'm attempting to integrate modules with sol. My first test case is a module that modifies a window's style. The module works fine if I treated the HWND as a number (Win64 still uses 32-bit window handles) but I am having trouble storing it as userdata, which will be better for cross-platform compatibility, since Linux and Mac use pointers for window handles, which cannot fit in a Lua number.
In the main program:
Window::GetHandle returns a win32 HWND, which I cast to a LuaWindowHandle pointer so that sol recognizes the type.
In my Lua module, I have this code:
luaL_checkudata is producing a type error. If I call luaL_typename(L, 1) it returns a big integer. I don't know what that number is, but it isn't the HWND.
Can you tell what I am doing wrong?