lua-users home
lua-l archive

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


On Thu, Feb 21, 2013 at 8:29 PM, Finn Wilcox <finnw@firecloudsw.co.uk> wrote:
> I have a FILE* which I would like to pass from a C function to a Lua
> function, such that the Lua code can treat it as an io.* file (i.e. call
> methods on it like file:read(), file:seek(), file:close() etc.)


As you already mentioned in another message, it's unfortunately
version-dependent.

but, there are different levels of compliance that you can aim to.

1: total interchangeability.  i have done this but limited to only one
VM (5.1 in my case). it certainly works if you replicate the lauxlib
code.

2: mostly interchangeable:  i haven't tried, but if you don't care
about letting io.close() deal with your file, it seems that a simple
userdata with the appropriate metatable is common across VMs

3: walk like a duck: write your own userdata with your own metatable
with read(), seek(), close(), etc methods.  it won't work with
functions that expect a file object (io.type(), io.output(),
io.close()), but any code that does OOP-like method calling
(file:write(), file:close(), file:seek()) won't tell the difference.



-- 
Javier