From a7f8564424cc1d3171f366bf0eefc1ec90b1b1ca Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Fri, 14 Feb 2020 09:10:25 +0000 Subject: [PATCH] file: raise error on .on() with non-function/nil Seems more polite than quietly accepting other types as nil. --- app/modules/file.c | 12 +++++++++--- docs/modules/file.md | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/modules/file.c b/app/modules/file.c index db717076..b5f14292 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -88,15 +88,21 @@ static int file_on(lua_State *L) case ON_RTC: luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref); - if ((lua_type(L, 2) == LUA_TFUNCTION) || - (lua_type(L, 2) == LUA_TLIGHTFUNCTION)) { + switch(lua_type(L, 2)) { + case LUA_TFUNCTION: + case LUA_TLIGHTFUNCTION: lua_pushvalue(L, 2); // copy argument (func) to the top of stack rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); vfs_register_rtc_cb(file_rtc_cb); - } else { + break; + case LUA_TNIL: rtc_cb_ref = LUA_NOREF; vfs_register_rtc_cb(NULL); + break; + default: + luaL_error(L, "Callback should be function or nil"); } + break; default: break; diff --git a/docs/modules/file.md b/docs/modules/file.md index 75ecc2ef..4426e979 100644 --- a/docs/modules/file.md +++ b/docs/modules/file.md @@ -222,7 +222,7 @@ Trigger events are: #### Parameters - `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 `nil`