file: raise error on .on() with non-function/nil

Seems more polite than quietly accepting other types as nil.
This commit is contained in:
Nathaniel Wesley Filardo 2020-02-14 09:10:25 +00:00 committed by Marcel Stör
parent 31c71c0e5d
commit a7f8564424
2 changed files with 10 additions and 4 deletions

View File

@ -88,15 +88,21 @@ static int file_on(lua_State *L)
case ON_RTC: case ON_RTC:
luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref); luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref);
if ((lua_type(L, 2) == LUA_TFUNCTION) || switch(lua_type(L, 2)) {
(lua_type(L, 2) == LUA_TLIGHTFUNCTION)) { case LUA_TFUNCTION:
case LUA_TLIGHTFUNCTION:
lua_pushvalue(L, 2); // copy argument (func) to the top of stack lua_pushvalue(L, 2); // copy argument (func) to the top of stack
rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
vfs_register_rtc_cb(file_rtc_cb); vfs_register_rtc_cb(file_rtc_cb);
} else { break;
case LUA_TNIL:
rtc_cb_ref = LUA_NOREF; rtc_cb_ref = LUA_NOREF;
vfs_register_rtc_cb(NULL); vfs_register_rtc_cb(NULL);
break;
default:
luaL_error(L, "Callback should be function or nil");
} }
break; break;
default: default:
break; break;

View File

@ -222,7 +222,7 @@ Trigger events are:
#### Parameters #### Parameters
- `event` string - `event` string
- `function()` callback function. Unregisters the callback if `function()` is omitted. - `function()` callback function. Unregisters the callback if `function()` is omitted or `nil`.
#### Returns #### Returns
`nil` `nil`