]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
FFI: Add ffi.errno().
authorMike Pall <mike>
Sun, 8 May 2011 20:34:40 +0000 (22:34 +0200)
committerMike Pall <mike>
Sun, 8 May 2011 20:34:40 +0000 (22:34 +0200)
doc/ext_ffi_api.html
src/lib_ffi.c

index b1b428783f6a64db0346a23dddc57539752d5819..2d69cb49a943611affe21ac8db6829ac994bbeab 100644 (file)
@@ -336,6 +336,28 @@ objects.
 
 <h2 id="util">Utility Functions</h2>
 
+<h3 id="ffi_errno"><tt>err = ffi.errno()</tt></h3>
+<p>
+Returns the error number set by the last C&nbsp;function call which
+indicated an error condition.
+</p>
+<p>
+This function offers a portable and OS-independent way to get the 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
+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
+value.
+</p>
+<p>
+You're advised to call this function only when needed and as close as
+possible after the return of the related C&nbsp;function. The
+<tt>errno</tt> value is preserved across hooks, memory allocations,
+invocations of the JIT compiler and other internal VM activity. The same
+applies to the value returned by <tt>GetLastError()</tt> on Windows, but
+you need to declare and call it yourself.
+</p>
+
 <h3 id="ffi_string"><tt>str = ffi.string(ptr [,len])</tt></h3>
 <p>
 Creates an interned Lua string from the data pointed to by
index 27996f0e42afebd3bd49c6582a74482168ece818..321de4998d3463eb0746bb630bf2800244fa37e8 100644 (file)
@@ -6,6 +6,8 @@
 #define lib_ffi_c
 #define LUA_LIB
 
+#include <errno.h>
+
 #include "lua.h"
 #include "lauxlib.h"
 #include "lualib.h"
@@ -541,6 +543,12 @@ LJLIB_CF(ffi_offsetof)
   return 0;
 }
 
+LJLIB_CF(ffi_errno)
+{
+  setintV(L->top++, errno);
+  return 1;
+}
+
 LJLIB_CF(ffi_string)   LJLIB_REC(.)
 {
   CTState *cts = ctype_cts(L);