@@ -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+ }
0 commit comments