Skip to content

Commit c24b118

Browse files
committed
Expose number of expected C call results from Lua
1 parent 70f5182 commit c24b118

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

vendor/lua/src/lapi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,3 +1134,9 @@ LUA_API void lua_addtotalbytes(lua_State *L, int n)
11341134
global_State *g = G(L);
11351135
g->totalbytes += n;
11361136
}
1137+
1138+
// MTA addition to access expected results
1139+
LUA_API int lua_ncallresult(lua_State *L)
1140+
{
1141+
return L->nexpectedresults;
1142+
}

vendor/lua/src/ldo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
335335
allowed = pPreCallHook ( *curr_func(L)->c.f, L );
336336
if ( allowed )
337337
{
338+
// MTA Specific: Store number of expected results for lua_ncallresult
339+
L->nexpectedresults = nresults;
338340
n = (*curr_func(L)->c.f)(L); /* do the actual call */
339341
// MTA Specific
340342
if ( pPostCallHook )

vendor/lua/src/lstate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ struct lua_State {
124124
GCObject *gclist;
125125
struct lua_longjmp *errorJmp; /* current error recover point */
126126
ptrdiff_t errfunc; /* current error handling function (stack index) */
127+
int nexpectedresults; /* MTA specific: Number of expected results from a C call. Only valid inside a function call.
128+
May no longer be valid if any further Lua calls were made (e.g. via lua_pcall) */
127129
};
128130

129131

vendor/lua/src/lua.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
220220
const char *chunkname);
221221

222222
LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
223-
223+
// MTA specific: Returns the number of expected results in a C call.
224+
// Note that this will no longer be reliable if
225+
// It will also not be reliable in case of incorrectly called functions
226+
// e.g.
227+
// local a, b = tostring(3)
228+
// will return 2, despite tostring only returning one number
229+
LUA_API int (lua_ncallresult) (lua_State* L);
224230

225231
/*
226232
** coroutine functions

0 commit comments

Comments
 (0)