lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:
Is there a clean way of creating new handles that'll work with the other
functions in the io library, such that one could write an extension and
put it in such a library?

Yes.

If so, where is it documented?


Right here :) You must create a userdata containing only the FILE* and
set its metatable to the value at REGISTRY["FILE*"]:

  FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
  *pf = popen or whatever...
  luaL_getmetatable(L, LUA_FILEHANDLE);
  lua_setmetatable(L, -2);

It'd be nice if things like this were documented somewhere other than the source. It's also a bit of a shame I can't provide a FILE* for reading, and one for writing - I'd quite like a bidirectional popen :)

What's wrong with just having another flag in the userdata to say it's
a popen,


That needs special support from iolib. Even if we agree to put popen
back there, we still have the problem for other external file handles
(e.g., pipes).

I assume you mean pipes in the popen way, than pipes say produced by the pipe() call in POSIX?

How about extending the userdata that iolib uses such that it contains a pointer to a function to close the handle? Or actually, a step better than that - abstract the whole lot so it's easy for people to create IO methods that use the same interface?

(It'd be nice to use the same interface for files, sockets, popen, etc!)

--
Rob Kendrick, Pepperfish Ltd.         +44 (0)845 226 4146  www.pepperfish.net
PGP signed or encrypted mail welcome                         Key ID: 3651D17A