Skip to content

Commit

Permalink
Merge pull request #727 from javalikescript/thread-arg-integer
Browse files Browse the repository at this point in the history
Support Lua integer in thread and async arguments
  • Loading branch information
squeek502 authored Nov 5, 2024
2 parents d38dd0a + 0d141bf commit 9299a4b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/lthreadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ typedef struct {
int type;
union
{
lua_Number num;
struct {
int isinteger;
union {
lua_Number n;
lua_Integer i;
} value;
} num;
int boolean;
struct {
const char* base;
Expand Down
13 changes: 11 additions & 2 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ static int luv_thread_arg_set(lua_State* L, luv_thread_arg_t* args, int idx, int
arg->val.boolean = lua_toboolean(L, i);
break;
case LUA_TNUMBER:
arg->val.num = lua_tonumber(L, i);
arg->val.num.isinteger = lua_isinteger(L, i);
if (arg->val.num.isinteger) {
arg->val.num.value.i = lua_tointeger(L, i);
} else {
arg->val.num.value.n = lua_tonumber(L, i);
}
break;
case LUA_TSTRING:
if (async)
Expand Down Expand Up @@ -181,7 +186,11 @@ static int luv_thread_arg_push(lua_State* L, luv_thread_arg_t* args, int flags)
lua_pushboolean(L, arg->val.boolean);
break;
case LUA_TNUMBER:
lua_pushnumber(L, arg->val.num);
if (arg->val.num.isinteger) {
lua_pushinteger(L, arg->val.num.value.i);
} else {
lua_pushnumber(L, arg->val.num.value.n);
}
break;
case LUA_TSTRING:
lua_pushlstring(L, arg->val.str.base, arg->val.str.len);
Expand Down
9 changes: 7 additions & 2 deletions tests/test-thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ return require('lib/tap')(function (test)
local delay = 100
uv.update_time()
local before = uv.now()
local args = {delay, 'string', nil, false, 5, "helloworld"}
local args = {delay, 'string', nil, false, 5, 3.14, "helloworld"}
local unpack = unpack or table.unpack
uv.new_thread({stack_size=0}, function(delay,s,null,bool,five,hw)
uv.new_thread({stack_size=0}, function(delay,s,null,bool,five,pi,hw)
assert(type(delay) == "number")
assert(type(s) == "string")
assert(null == nil)
assert(bool == false)
assert(five == 5)
assert(tostring(five) == '5', 'invalid integer to string ('..tostring(five)..')')
if math.type then
assert(math.type(five) == 'integer', 'invalid integer')
end
assert(pi == 3.14, 'invalid decimal number')
assert(hw == 'helloworld')
require('luv').sleep(delay)
end, unpack(args)):join()
Expand Down

0 comments on commit 9299a4b

Please sign in to comment.