FFI: Allow setting errno with ffi.errno(), too.

This commit is contained in:
Mike Pall 2011-05-09 11:51:19 +02:00
parent 868ecad32b
commit d9c1f771a7
2 changed files with 10 additions and 5 deletions

View File

@ -336,14 +336,16 @@ objects.
<h2 id="util">Utility Functions</h2> <h2 id="util">Utility Functions</h2>
<h3 id="ffi_errno"><tt>err = ffi.errno()</tt></h3> <h3 id="ffi_errno"><tt>err = ffi.errno([newerr])</tt></h3>
<p> <p>
Returns the error number set by the last C&nbsp;function call which Returns the error number set by the last C&nbsp;function call which
indicated an error condition. indicated an error condition. If the optional <tt>newerr</tt> argument
is present, the error number is set to the new value and the previous
value is returned.
</p> </p>
<p> <p>
This function offers a portable and OS-independent way to get the error This function offers a portable and OS-independent way to get and set the
number. Note that only <em>some</em> C&nbsp;functions set the error error number. Note that only <em>some</em> C&nbsp;functions set the error
number. And it's only significant if the function actually indicated an number. And it's only significant if the function actually indicated an
error condition (e.g. with a return value of <tt>-1</tt> or error condition (e.g. with a return value of <tt>-1</tt> or
<tt>NULL</tt>). Otherwise, it may or may not contain any previously set <tt>NULL</tt>). Otherwise, it may or may not contain any previously set

View File

@ -545,7 +545,10 @@ LJLIB_CF(ffi_offsetof)
LJLIB_CF(ffi_errno) LJLIB_CF(ffi_errno)
{ {
setintV(L->top++, errno); int err = errno;
if (L->top > L->base)
errno = ffi_checkint(L, 1);
setintV(L->top++, err);
return 1; return 1;
} }