diff --git a/src/lcmulti.c b/src/lcmulti.c index ca6941a..4bfc1e7 100644 --- a/src/lcmulti.c +++ b/src/lcmulti.c @@ -380,7 +380,7 @@ static int lcurl_multi_timeout(lua_State *L){ static int lcurl_multi_socket_action(lua_State *L){ lcurl_multi_t *p = lcurl_getmulti(L); - curl_socket_t s = lutil_optint64(L, 2, CURL_SOCKET_TIMEOUT); + curl_socket_t s = lcurl_opt_os_socket(L, 2, CURL_SOCKET_TIMEOUT); CURLMcode code; int n, mask; lua_State *curL; @@ -551,7 +551,7 @@ static int lcurl_multi_socket_callback(CURL *easy, curl_socket_t s, int what, vo lua_rawgetp(L, -1, easy); e = lcurl_geteasy_at(L, -1); lua_remove(L, -2); - lutil_pushint64(L, s); + lcurl_push_os_socket(L, s); lua_pushinteger(L, what); if(lua_pcall(L, n+2, 0, 0)){ diff --git a/src/lcutils.c b/src/lcutils.c index 2747f9e..c6baa6f 100644 --- a/src/lcutils.c +++ b/src/lcutils.c @@ -332,4 +332,58 @@ void lcurl_stack_dump (lua_State *L){ i++; } fprintf(stderr, " ------------ Stack Dump Finished ------------\n" ); -} \ No newline at end of file +} + +curl_socket_t lcurl_opt_os_socket(lua_State *L, int idx, curl_socket_t def) { + if (lua_islightuserdata(L, idx)) + return (curl_socket_t)lua_touserdata(L, idx); + + return (curl_socket_t)lutil_optint64(L, idx, def); +} + +void lcurl_push_os_socket(lua_State *L, curl_socket_t fd) { +#if !defined(_WIN32) + lutil_pushint64(L, fd); +#else /*_WIN32*/ + /* Assumes that compiler can optimize constant conditions. MSVC do this. */ + + /*On Lua 5.3 lua_Integer type can be represented exactly*/ +#if LUA_VERSION_NUM >= 503 + if (sizeof(curl_socket_t) <= sizeof(lua_Integer)) { + lua_pushinteger(L, (lua_Integer)fd); + return; + } +#endif + +#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) + /*! @todo test DBL_MANT_DIG, FLT_MANT_DIG */ + + if (sizeof(lua_Number) == 8) { /*we have 53 bits for integer*/ + if ((sizeof(curl_socket_t) <= 6)) { + lua_pushnumber(L, (lua_Number)fd); + return; + } + + if(((UINT_PTR)fd & 0x1FFFFFFFFFFFFF) == (UINT_PTR)fd) + lua_pushnumber(L, (lua_Number)fd); + else + lua_pushlightuserdata(L, (void*)fd); + + return; + } + + if (sizeof(lua_Number) == 4) { /*we have 24 bits for integer*/ + if (((UINT_PTR)fd & 0xFFFFFF) == (UINT_PTR)fd) + lua_pushnumber(L, (lua_Number)fd); + else + lua_pushlightuserdata(L, (void*)fd); + return; + } +#endif + + lutil_pushint64(L, fd); + if (lcurl_opt_os_socket(L, -1, 0) != fd) + lua_pushlightuserdata(L, (void*)fd); + +#endif /*_WIN32*/ +} diff --git a/src/lcutils.h b/src/lcutils.h index bc342a4..25a822d 100644 --- a/src/lcutils.h +++ b/src/lcutils.h @@ -91,4 +91,8 @@ int lcurl_utils_apply_options(lua_State *L, int opt, int obj, int do_close, void lcurl_stack_dump (lua_State *L); +curl_socket_t lcurl_opt_os_socket(lua_State *L, int idx, curl_socket_t def); + +void lcurl_push_os_socket(lua_State *L, curl_socket_t fd); + #endif