lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 22/02/2013 01:45, mlepage@antimeta.com wrote:
> I would look at the source for the file lib and see what it expects.
> I am guessing a userdata with metatable. If you can make the same
> sort of object, it should Just Work.
> 
Yes, in the three implementations I have looked at so far, it is always
a userdata, and the payload always starts with a FILE*

But after that, it varies depending on the implementation:
- Lua 5.2 has a pointer to a close function following the FILE* in the
payload
- Lua 5.1 keeps the close function pointer in the uservalue (the payload
contains only the FILE*)
- LuaJIT 2.0 adds an enum to the payload instead of a function pointer.
 It also has a tag in the header indicating that this userdata is a file
(which you cannot fake using lua_newuserdata.)

Maybe it is still possible to make this work in all 3 versions?

For example, you could open a dummy file on the Lua side (tmpfile or
/dev/null) then replace the FILE* with the supplied one and close the
original.

Or use a combination of fileno and fdup2, to keep the same FILE* but
replace the underlying FD.