[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Wrapping existing FILE *s as Lua file objects
- From: Mark Edgar <medgar@...>
- Date: Sun, 14 Jan 2007 00:27:42 -0700
Reuben Thomas wrote:
I can't see a way to do this after looking at liolib.c and googling a
bit. It's obviously fairly trivial to extend liolib.c to do it, but a
pain as you can't do it with vanilla Lua.
Am I missing something? I don't imagine it's that rare to want to pass a
FILE * obtained from somewhere else into a Lua script without
re-inventing the liolib wheel...
The ex implementations do this for the io.pipe() function, which returns
two Lua file objects.
http://lua-users.org/wiki/ExtensionProposal
FILE **pf = lua_newuserdata(L, sizeof *pf);
*pf = 0;
luaL_getmetatable(L, LUA_FILEHANDLE);
lua_setmetatable(L, -2);
*pf = /* your FILE* here */
In Lua 5.1, LUA_FILEHANDLE is defined in lualib.h.
-Mark