Skip to content

Commit d0cde48

Browse files
authored
Merge pull request #108 from moteus/master
Add. Pass socket as lightuserdata if it not fit to Lua number.
2 parents eb4c503 + 3157340 commit d0cde48

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/lcmulti.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static int lcurl_multi_timeout(lua_State *L){
380380

381381
static int lcurl_multi_socket_action(lua_State *L){
382382
lcurl_multi_t *p = lcurl_getmulti(L);
383-
curl_socket_t s = lutil_optint64(L, 2, CURL_SOCKET_TIMEOUT);
383+
curl_socket_t s = lcurl_opt_os_socket(L, 2, CURL_SOCKET_TIMEOUT);
384384
CURLMcode code; int n, mask;
385385
lua_State *curL;
386386

@@ -551,7 +551,7 @@ static int lcurl_multi_socket_callback(CURL *easy, curl_socket_t s, int what, vo
551551
lua_rawgetp(L, -1, easy);
552552
e = lcurl_geteasy_at(L, -1);
553553
lua_remove(L, -2);
554-
lutil_pushint64(L, s);
554+
lcurl_push_os_socket(L, s);
555555
lua_pushinteger(L, what);
556556

557557
if(lua_pcall(L, n+2, 0, 0)){

src/lcutils.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,58 @@ void lcurl_stack_dump (lua_State *L){
332332
i++;
333333
}
334334
fprintf(stderr, " ------------ Stack Dump Finished ------------\n" );
335-
}
335+
}
336+
337+
curl_socket_t lcurl_opt_os_socket(lua_State *L, int idx, curl_socket_t def) {
338+
if (lua_islightuserdata(L, idx))
339+
return (curl_socket_t)lua_touserdata(L, idx);
340+
341+
return (curl_socket_t)lutil_optint64(L, idx, def);
342+
}
343+
344+
void lcurl_push_os_socket(lua_State *L, curl_socket_t fd) {
345+
#if !defined(_WIN32)
346+
lutil_pushint64(L, fd);
347+
#else /*_WIN32*/
348+
/* Assumes that compiler can optimize constant conditions. MSVC do this. */
349+
350+
/*On Lua 5.3 lua_Integer type can be represented exactly*/
351+
#if LUA_VERSION_NUM >= 503
352+
if (sizeof(curl_socket_t) <= sizeof(lua_Integer)) {
353+
lua_pushinteger(L, (lua_Integer)fd);
354+
return;
355+
}
356+
#endif
357+
358+
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
359+
/*! @todo test DBL_MANT_DIG, FLT_MANT_DIG */
360+
361+
if (sizeof(lua_Number) == 8) { /*we have 53 bits for integer*/
362+
if ((sizeof(curl_socket_t) <= 6)) {
363+
lua_pushnumber(L, (lua_Number)fd);
364+
return;
365+
}
366+
367+
if(((UINT_PTR)fd & 0x1FFFFFFFFFFFFF) == (UINT_PTR)fd)
368+
lua_pushnumber(L, (lua_Number)fd);
369+
else
370+
lua_pushlightuserdata(L, (void*)fd);
371+
372+
return;
373+
}
374+
375+
if (sizeof(lua_Number) == 4) { /*we have 24 bits for integer*/
376+
if (((UINT_PTR)fd & 0xFFFFFF) == (UINT_PTR)fd)
377+
lua_pushnumber(L, (lua_Number)fd);
378+
else
379+
lua_pushlightuserdata(L, (void*)fd);
380+
return;
381+
}
382+
#endif
383+
384+
lutil_pushint64(L, fd);
385+
if (lcurl_opt_os_socket(L, -1, 0) != fd)
386+
lua_pushlightuserdata(L, (void*)fd);
387+
388+
#endif /*_WIN32*/
389+
}

src/lcutils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ int lcurl_utils_apply_options(lua_State *L, int opt, int obj, int do_close,
9191

9292
void lcurl_stack_dump (lua_State *L);
9393

94+
curl_socket_t lcurl_opt_os_socket(lua_State *L, int idx, curl_socket_t def);
95+
96+
void lcurl_push_os_socket(lua_State *L, curl_socket_t fd);
97+
9498
#endif

0 commit comments

Comments
 (0)