Skip to content

Commit 20089cc

Browse files
committed
Allow timers with an interval of 0 (it means ASAP)
1 parent 17508de commit 20089cc

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

VM/src/llltimers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ static int lltimers_on(lua_State *L)
122122
luaL_typeerror(L, 3, "function or callable table");
123123
lua_settop(L, 3);
124124

125-
if (seconds <= 0.0)
126-
luaL_errorL(L, "timer interval must be positive");
125+
if (seconds < 0.0)
126+
luaL_errorL(L, "timer interval must be positive or 0");
127127

128128
// Get current time
129129
double current_time = sl_state->clockProvider ? sl_state->clockProvider(L) : 0.0;
@@ -176,8 +176,8 @@ static int lltimers_once(lua_State *L)
176176
luaL_typeerror(L, 3, "function or callable table");
177177
lua_settop(L, 3);
178178

179-
if (seconds <= 0.0)
180-
luaL_errorL(L, "timer interval must be positive");
179+
if (seconds < 0.0)
180+
luaL_errorL(L, "timer interval must be positive or 0");
181181

182182
// Get current time
183183
double current_time = sl_state->clockProvider ? sl_state->clockProvider(L) : 0.0;

tests/conformance/lltimers.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ local success, err = pcall(function()
135135
end)
136136
assert(success == false)
137137

138-
-- Test zero interval
139-
success, err = pcall(function()
140-
LLTimers:on(0, function() end)
141-
end)
142-
assert(success == false)
138+
-- Test zero interval (0 means "ASAP" and is valid)
139+
timer1_count = 0
140+
local function zero_interval_handler()
141+
timer1_count += 1
142+
end
143+
LLTimers:on(0, zero_interval_handler)
144+
LLTimers:_tick()
145+
assert(timer1_count == 1)
146+
LLTimers:off(zero_interval_handler)
143147

144148
-- Test invalid handler type
145149
success, err = pcall(function()

0 commit comments

Comments
 (0)