1 /* Copyright (c) 2010-2020. The SimGrid Team.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * This file contains functions that aid users to debug their lua scripts; for instance,
9 * tables can be easily output and values are represented in a human-readable way. (For instance,
10 * a nullptr value becomes the string "nil").
13 /* SimGrid Lua helper functions */
14 #include "lua_utils.hpp"
18 * @brief Returns a string representation of a value in the Lua stack.
20 * This function is for debugging purposes.
21 * It always returns the same pointer.
23 * @param L the Lua state
24 * @param index index in the stack
25 * @return a string representation of the value at this index
27 const char* sglua_tostring(lua_State* L, int index)
31 switch (lua_type(L, index)) {
33 snprintf(buff, 4, "nil");
37 snprintf(buff, 64, "%.3f", lua_tonumber(L, index));
41 snprintf(buff, 64, "%s", lua_toboolean(L, index) ? "true" : "false");
45 snprintf(buff, 63, "'%s'", lua_tostring(L, index));
49 if (lua_iscfunction(L, index)) {
50 snprintf(buff, 11, "C-function");
52 snprintf(buff, 9, "function");
57 snprintf(buff, 64, "table(%p)", lua_topointer(L, index));
60 case LUA_TLIGHTUSERDATA:
62 snprintf(buff, 64, "userdata(%p)", lua_touserdata(L, index));
66 snprintf(buff, 7, "thread");
70 snprintf(buff, 64, "unknown(%d)", lua_type(L, index));
77 * @brief Returns a string representation of a key-value pair.
79 * It always returns the same pointer.
81 * @param L the Lua state
82 * @param key_index index of the key (in the lua stack)
83 * @param value_index index of the value (in the lua stack)
84 * @return a string representation of the key-value pair
86 const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index)
89 /* value_tostring also always returns the same pointer */
90 int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index));
91 snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index));