fix stack checking for coroutines

This commit is contained in:
Isaac0-dev 2025-06-18 15:09:41 +10:00
parent f587dc38be
commit b9db3d5416
6 changed files with 49 additions and 49 deletions

View file

@ -24,13 +24,13 @@
#define LOG_LUA_LINE_WARNING(...) { if (!gLuaActiveMod->showedScriptWarning) { gLuaActiveMod->showedScriptWarning = true; smlua_mod_warning(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_WARNING); } }
#ifdef DEVELOPMENT
#define LUA_STACK_CHECK_BEGIN_NUM(n) int __LUA_STACK_TOP = lua_gettop(gLuaState) + (n)
#define LUA_STACK_CHECK_BEGIN() LUA_STACK_CHECK_BEGIN_NUM(0)
#define LUA_STACK_CHECK_END() if ((__LUA_STACK_TOP) != lua_gettop(gLuaState)) { smlua_dump_stack(); fflush(stdout); } assert((__LUA_STACK_TOP) == lua_gettop(gLuaState))
#define LUA_STACK_CHECK_BEGIN_NUM(state, n) int __LUA_STACK_TOP = lua_gettop(state) + (n)
#define LUA_STACK_CHECK_BEGIN(state) LUA_STACK_CHECK_BEGIN_NUM(state, 0)
#define LUA_STACK_CHECK_END(state) if ((__LUA_STACK_TOP) != lua_gettop(state)) { smlua_dump_stack(); fflush(stdout); } assert((__LUA_STACK_TOP) == lua_gettop(state))
#else
#define LUA_STACK_CHECK_BEGIN_NUM(n)
#define LUA_STACK_CHECK_BEGIN()
#define LUA_STACK_CHECK_END()
#define LUA_STACK_CHECK_BEGIN_NUM(state, n)
#define LUA_STACK_CHECK_BEGIN(state)
#define LUA_STACK_CHECK_END(state)
#endif
extern lua_State* gLuaState;

View file

@ -183,7 +183,7 @@ static const char *smlua_get_custom_field_type_name(enum LuaValueType lvt) {
}
static int smlua_func_define_custom_obj_fields(lua_State* L) {
LUA_STACK_CHECK_BEGIN();
LUA_STACK_CHECK_BEGIN(L);
if (!smlua_functions_valid_param_count(L, 1)) { return 0; }
if (lua_type(L, 1) != LUA_TTABLE) {
@ -295,12 +295,12 @@ static int smlua_func_define_custom_obj_fields(lua_State* L) {
lua_pop(L, 1); // pop key
lua_pop(L, 1); // pop _custom_object_fields
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}
struct LuaObjectField* smlua_get_custom_field(lua_State* L, u32 lot, int keyIndex) {
LUA_STACK_CHECK_BEGIN();
LUA_STACK_CHECK_BEGIN(L);
static struct LuaObjectField lof = { 0 };
if (lot != LOT_OBJECT) { return NULL; }
@ -323,7 +323,7 @@ struct LuaObjectField* smlua_get_custom_field(lua_State* L, u32 lot, int keyInde
if (lua_type(L, -1) != LUA_TTABLE) {
lua_pop(L, 1); // pop value table
lua_pop(L, 1); // pop _custom_fields
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return NULL;
}
@ -336,7 +336,7 @@ struct LuaObjectField* smlua_get_custom_field(lua_State* L, u32 lot, int keyInde
if (!gSmLuaConvertSuccess || !validFieldIndex) {
lua_pop(L, 1); // pop value table
lua_pop(L, 1); // pop _custom_fields
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return NULL;
}
@ -349,7 +349,7 @@ struct LuaObjectField* smlua_get_custom_field(lua_State* L, u32 lot, int keyInde
if (!gSmLuaConvertSuccess || !validLvt) {
lua_pop(L, 1); // pop value table
lua_pop(L, 1); // pop _custom_fields
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return NULL;
}
@ -363,7 +363,7 @@ struct LuaObjectField* smlua_get_custom_field(lua_State* L, u32 lot, int keyInde
lua_pop(L, 1); // pop value table
lua_pop(L, 1); // pop _custom_fields
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return &lof;
}
@ -471,7 +471,7 @@ static bool smlua_set_field(lua_State* L, u8* p, struct LuaObjectField *data) {
}
static int smlua__get_field(lua_State* L) {
LUA_STACK_CHECK_BEGIN_NUM(1);
LUA_STACK_CHECK_BEGIN_NUM(L, 1);
const CObject *cobj = lua_touserdata(L, 1);
enum LuaObjectType lot = cobj->lot;
@ -524,7 +524,7 @@ static int smlua__get_field(lua_State* L) {
return 0;
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}
@ -569,12 +569,12 @@ static int smlua__get_field(lua_State* L) {
}
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}
static int smlua__set_field(lua_State* L) {
LUA_STACK_CHECK_BEGIN();
LUA_STACK_CHECK_BEGIN(L);
const CObject *cobj = lua_touserdata(L, 1);
enum LuaObjectType lot = cobj->lot;
@ -610,7 +610,7 @@ static int smlua__set_field(lua_State* L) {
return 0;
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}
@ -645,7 +645,7 @@ static int smlua__set_field(lua_State* L) {
return 0;
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}

View file

@ -44,7 +44,7 @@ bool smlua_functions_valid_param_range(lua_State* L, int min, int max) {
///////////
int smlua_func_table_copy(lua_State *L) {
LUA_STACK_CHECK_BEGIN_NUM(1);
LUA_STACK_CHECK_BEGIN_NUM(L, 1);
if (!smlua_functions_valid_param_count(L, 1)) { return 0; }
@ -70,7 +70,7 @@ int smlua_func_table_copy(lua_State *L) {
lua_settable(L, 2);
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}
@ -128,7 +128,7 @@ static void table_deepcopy_table(lua_State *L, int idxTable, int idxCache) {
}
int smlua_func_table_deepcopy(lua_State *L) {
LUA_STACK_CHECK_BEGIN_NUM(1);
LUA_STACK_CHECK_BEGIN_NUM(L, 1);
if (!smlua_functions_valid_param_count(L, 1)) { return 0; }
@ -145,7 +145,7 @@ int smlua_func_table_deepcopy(lua_State *L) {
lua_remove(L, idxCache);
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}

View file

@ -1032,7 +1032,7 @@ bool smlua_subcommand_exists(const char* maincommand, const char* subcommand) {
//////////////////////////////
int smlua_hook_on_sync_table_change(lua_State* L) {
LUA_STACK_CHECK_BEGIN();
LUA_STACK_CHECK_BEGIN(L);
if (L == NULL) { return 0; }
if(!smlua_functions_valid_param_count(L, 4)) { return 0; }
@ -1082,7 +1082,7 @@ int smlua_hook_on_sync_table_change(lua_State* L) {
lua_remove(L, hookOnChangedIndex);
lua_remove(L, valTableIndex);
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return 1;
}

View file

@ -37,8 +37,8 @@ static void smlua_sync_table_create(u16 modRemoteIndex, enum LuaSyncTableType ls
}
static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
sUnwoundLntsCount = 0;
// get key
@ -110,13 +110,13 @@ static bool smlua_sync_table_unwind(int syncTableIndex, int keyIndex) {
return false;
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return true;
}
static void smlua_sync_table_call_hook(int syncTableIndex, int keyIndex, int prevValueIndex, int valueIndex) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
// get hook table
lua_pushstring(L, "_hook_on_changed"); lua_rawget(L, syncTableIndex);
@ -160,12 +160,12 @@ static void smlua_sync_table_call_hook(int syncTableIndex, int keyIndex, int pre
}
lua_pop(L, 1); // pop _hook_on_changed's value
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool alterSeq) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
int syncTableIndex = stackIndex + 1;
int keyIndex = stackIndex + 2;
@ -299,7 +299,7 @@ static bool smlua_sync_table_send_field(u8 toLocalIndex, int stackIndex, bool al
CLEANUP_STACK:
lua_remove(L, prevValueIndex); // pop prevValue
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return ret;
}
@ -310,8 +310,8 @@ static int smlua__set_sync_table_field(lua_State* L) {
}
void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 lntKeyCount, struct LSTNetworkType* lntKeys, struct LSTNetworkType* lntValue) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
// figure out entry
if (modRemoteIndex >= gActiveMods.entryCount) {
@ -441,12 +441,12 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 ln
lua_pop(L, 1); // pop prevValue
lua_pop(L, 1); // pop internal table
lua_pop(L, syncTableSize); // pop sync table
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
void smlua_sync_table_init_globals(const char* path, u16 modRemoteIndex) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
lua_getfield(L, LUA_REGISTRYINDEX, path);
int fileGlobalIndex = lua_gettop(L);
@ -491,22 +491,22 @@ void smlua_sync_table_init_globals(const char* path, u16 modRemoteIndex) {
}
lua_pop(L, 1); // pop file's "global" table
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
void smlua_bind_sync_table(void) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
smlua_bind_function(L, "_set_sync_table_field", smlua__set_sync_table_field);
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
////////////////////////////////////////////////
static void smlua_sync_table_send_table(u8 toLocalIndex) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
int tableIndex = lua_gettop(L);
lua_getfield(L, -1, "_table");
@ -530,12 +530,12 @@ static void smlua_sync_table_send_table(u8 toLocalIndex) {
lua_pop(L, 1); // remove internal table
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
static void smlua_sync_table_send_all_file(u8 toLocalIndex, const char* path) {
LUA_STACK_CHECK_BEGIN();
lua_State* L = gLuaState;
LUA_STACK_CHECK_BEGIN(L);
LOG_INFO("sending sync table for file %s to %u", path, toLocalIndex);
@ -568,15 +568,15 @@ static void smlua_sync_table_send_all_file(u8 toLocalIndex, const char* path) {
lua_pop(L, 1); // pop file's "global" table
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
void smlua_sync_table_send_all(u8 toLocalIndex) {
SOFT_ASSERT(gNetworkType == NT_SERVER);
LUA_STACK_CHECK_BEGIN();
LUA_STACK_CHECK_BEGIN(gLuaState);
for (int i = 0; i < gActiveMods.entryCount; i++) {
struct Mod* mod = gActiveMods.entries[i];
smlua_sync_table_send_all_file(toLocalIndex, mod->relativePath);
}
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(gLuaState);
}

View file

@ -380,7 +380,7 @@ CObject *smlua_push_object(lua_State* L, u16 lot, void* p, void *extraInfo) {
lua_pushnil(L);
return NULL;
}
LUA_STACK_CHECK_BEGIN_NUM(1);
LUA_STACK_CHECK_BEGIN_NUM(L, 1);
uintptr_t key = (lot * 0x9E3779B97F4A7C15) ^ ((uintptr_t)p >> 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, gSmLuaCObjects);
@ -407,7 +407,7 @@ CObject *smlua_push_object(lua_State* L, u16 lot, void* p, void *extraInfo) {
lua_settable(L, -4);
lua_remove(L, -2); // Remove gSmLuaCObjects table
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return cobject;
}
@ -417,7 +417,7 @@ CPointer *smlua_push_pointer(lua_State* L, u16 lvt, void* p, void *extraInfo) {
lua_pushnil(L);
return NULL;
}
LUA_STACK_CHECK_BEGIN_NUM(1);
LUA_STACK_CHECK_BEGIN_NUM(L, 1);
uintptr_t key = (lvt * 0x9E3779B97F4A7C15) ^ ((uintptr_t)p >> 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, gSmLuaCPointers);
@ -443,7 +443,7 @@ CPointer *smlua_push_pointer(lua_State* L, u16 lvt, void* p, void *extraInfo) {
lua_pushvalue(L, -2); // Duplicate userdata
lua_settable(L, -4);
lua_remove(L, -2); // Remove gSmLuaCPointers table
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
return cpointer;
}
@ -808,7 +808,7 @@ void smlua_logline(void) {
void smlua_free(void *ptr) {
if (ptr && gLuaState) {
lua_State *L = gLuaState;
LUA_STACK_CHECK_BEGIN();
LUA_STACK_CHECK_BEGIN(L);
u16 lot = LOT_SURFACE; // Assuming this is a surface
uintptr_t key = lot ^ (uintptr_t) ptr;
lua_rawgeti(L, LUA_REGISTRYINDEX, gSmLuaCObjects);
@ -825,7 +825,7 @@ void smlua_free(void *ptr) {
lua_pop(L, 1);
}
lua_pop(L, 1);
LUA_STACK_CHECK_END();
LUA_STACK_CHECK_END(L);
}
free(ptr);
}