From 868ecad32ba7fb3eb763642e22ebb06938fdb359 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Sun, 8 May 2011 22:34:40 +0200 Subject: [PATCH] FFI: Add ffi.errno(). --- doc/ext_ffi_api.html | 22 ++++++++++++++++++++++ src/lib_ffi.c | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/doc/ext_ffi_api.html b/doc/ext_ffi_api.html index b1b42878..2d69cb49 100644 --- a/doc/ext_ffi_api.html +++ b/doc/ext_ffi_api.html @@ -336,6 +336,28 @@ objects.

Utility Functions

+

err = ffi.errno()

+

+Returns the error number set by the last C function call which +indicated an error condition. +

+

+This function offers a portable and OS-independent way to get the error +number. Note that only some C 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 -1 or +NULL). Otherwise, it may or may not contain any previously set +value. +

+

+You're advised to call this function only when needed and as close as +possible after the return of the related C function. The +errno 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 GetLastError() on Windows, but +you need to declare and call it yourself. +

+

str = ffi.string(ptr [,len])

Creates an interned Lua string from the data pointed to by diff --git a/src/lib_ffi.c b/src/lib_ffi.c index 27996f0e..321de499 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c @@ -6,6 +6,8 @@ #define lib_ffi_c #define LUA_LIB +#include + #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);