file: raise error on .on() with non-function/nil
Seems more polite than quietly accepting other types as nil.
This commit is contained in:
parent
31c71c0e5d
commit
a7f8564424
|
@ -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;
|
||||||
|
|
|
@ -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`
|
||||||
|
|
Loading…
Reference in New Issue