lua-users home
lua-l archive

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


Justin Cormack wrote:
> Mike Pall wrote:
> > So I've thought about adding an __errno attribute. If you declare
> > a C function with this attribute, you get the errno as an extra
> > return value (local ok, errno = ffi.C.mkdir(...)). This is fetched
> > by low-level code immediately after the function returns. Alas,
> > this means I have to figure out all of the variants for getting
> > the errno value myself and teach them to the JIT compiler. :-/
> 
> Any further thoughts about this? This method does sound nice and easy to
> use.

Well, it has a couple of drawbacks. It's hard to avoid fetching
the errno, in case it's not needed. And you can't just include a
header file -- you'd need to fix up all declarations.

> It does seem however that there should be platform dependent methods
> though, so maybe it is not necessary. On Linux the code below
> (__errno_location() is part of LSB) seems to work fine.

Sadly, this is unreliable. E.g. a memory allocation might happen
inbetween or a hook may be called or the JIT compiler might print
debug info inbetween and so on. All of this may reset errno.

I have a couple of ideas on how to provide a portable ffi.C.errno
abstraction which doesn't lose its state. But no convincing
solution. And then there's GetLastError() on Windows, too ...

--Mike