X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/4610b103f5b664648c716031782165da1e6fbd41..8f040b324eb3f808a1f5f8da203ae16b20296914:/src/bindings/lua/lua_utils.c diff --git a/src/bindings/lua/lua_utils.c b/src/bindings/lua/lua_utils.c index 55721bc7c4..efd257a417 100644 --- a/src/bindings/lua/lua_utils.c +++ b/src/bindings/lua/lua_utils.c @@ -6,11 +6,12 @@ /* SimGrid Lua helper functions */ +#include #include "lua_utils.h" #include "xbt.h" #include "xbt/log.h" -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_utils, lua, "Lua helper functions"); +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_utils, bindings, "Lua helper functions"); /** * @brief Returns a string representation of a value in the Lua stack. @@ -102,8 +103,11 @@ const char* sglua_get_spaces(int length) { static char spaces[128]; - xbt_assert(length < 128); - memset(spaces, ' ', length); + xbt_assert(length >= 0 && length < 128, + "Invalid indentation length: %d", length); + if (length != 0) { + memset(spaces, ' ', length); + } spaces[length] = '\0'; return spaces; } @@ -135,12 +139,44 @@ void sglua_stack_dump(const char* msg, lua_State* L) } } +/** + * \brief Like luaL_checkudata, with additional debug logs. + * + * This function is for debugging purposes only. + * + * \param L a lua state + * \param ud index of the userdata to check in the stack + * \param tname key of the metatable of this userdata in the registry + */ +void* sglua_checkudata_debug(lua_State* L, int ud, const char* tname) +{ + XBT_DEBUG("Checking the userdata: ud = %d", ud); + sglua_stack_dump("my_checkudata: ", L); + void* p = lua_touserdata(L, ud); + lua_getfield(L, LUA_REGISTRYINDEX, tname); + const void* correct_mt = lua_topointer(L, -1); + + int has_mt = lua_getmetatable(L, ud); + XBT_DEBUG("Checking the userdata: has metatable ? %d", has_mt); + const void* actual_mt = NULL; + if (has_mt) { + actual_mt = lua_topointer(L, -1); + lua_pop(L, 1); + } + XBT_DEBUG("Checking the task's metatable: expected %p, found %p", correct_mt, actual_mt); + sglua_stack_dump("my_checkudata: ", L); + + if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) + luaL_typerror(L, ud, tname); + lua_pop(L, 2); + return p; +} + /** * @brief Writes the specified data into a memory buffer. * * This function is a valid lua_Writer that writes into a memory buffer passed * as userdata. - * TODO: use a dynar as userdata * * @param L a lua state * @param source some data @@ -150,7 +186,7 @@ void sglua_stack_dump(const char* msg, lua_State* L) int sglua_memory_writer(lua_State* L, const void* source, size_t size, void* userdata) { - buffer_t buffer = (buffer_t) userdata; + sglua_buffer_t buffer = (sglua_buffer_t) userdata; while (buffer->capacity < buffer->size + size) { buffer->capacity *= 2; buffer->data = xbt_realloc(buffer->data, buffer->capacity);