Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support to get the return value(s) of lua callbacks #315

Merged
merged 3 commits into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions components/lua/common/sys.c
Expand Up @@ -69,7 +69,7 @@ lua_callback_t *luaS_callback_create(lua_State *L, int index) {
int argref = LUA_REFNIL;

if (lua_gettop(L) == index + 1) {
// Callback has argument
// Callback has argument
lua_pushvalue(L, index + 1);
argref = luaL_ref(L, LUA_REGISTRYINDEX);
}
Expand Down Expand Up @@ -105,20 +105,24 @@ lua_State *luaS_callback_state(lua_callback_t *callback) {
return callback->TL;
}

int luaS_callback_call(lua_callback_t *callback, int args) {
if (callback->arg != LUA_REFNIL) {
args++;
lua_rawgeti(callback->TL, LUA_REGISTRYINDEX, callback->arg );
}
int luaS_callback_call_return(lua_callback_t *callback, int args, int rets) {
if (callback->arg != LUA_REFNIL) {
args++;
lua_rawgeti(callback->TL, LUA_REGISTRYINDEX, callback->arg );
}

int rc = lua_pcall(callback->TL, args, 0, 0);
int rc = lua_pcall(callback->TL, args, rets, 0);

// Copy callback to thread
lua_pushvalue(callback->TL, 1);

return rc;
}

int luaS_callback_call(lua_callback_t *callback, int args) {
return luaS_callback_call_return(callback, args, 0);
}

void luaS_callback_destroy(lua_callback_t *callback) {
assert(callback != NULL);

Expand Down
12 changes: 11 additions & 1 deletion components/lua/common/sys.h
Expand Up @@ -84,10 +84,20 @@ lua_State *luaS_callback_state(lua_callback_t *callback);
*
* @param callback A pointer to a callback handler created with the luaS_callback_create
* function.
* @param args Number of argument of the function.
* @param args Number of arguments of the function.
*/
int luaS_callback_call(lua_callback_t *callback, int args);

/**
* @brief Call the callback's lua function.
*
* @param callback A pointer to a callback handler created with the luaS_callback_create
* function.
* @param args Number of arguments of the function.
* @param rets Number of return values of the function.
*/
int luaS_callback_call_return(lua_callback_t *callback, int args, int rets);

/**
* @brief Destroy a callback.
*
Expand Down