1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* SimGrid Lua helper functions */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_utils, bindings, "Lua helper functions");
16 * @brief Returns a string representation of a value in the Lua stack.
18 * This function is for debugging purposes.
19 * It always returns the same pointer.
21 * @param L the Lua state
22 * @param index index in the stack
23 * @return a string representation of the value at this index
25 const char* sglua_tostring(lua_State* L, int index) {
29 switch (lua_type(L, index)) {
36 sprintf(buff, "%.3f", lua_tonumber(L, index));
40 sprintf(buff, "%s", lua_toboolean(L, index) ? "true" : "false");
44 snprintf(buff, 63, "'%s'", lua_tostring(L, index));
48 if (lua_iscfunction(L, index)) {
49 sprintf(buff, "C-function");
52 sprintf(buff, "function");
57 sprintf(buff, "table(%p)", lua_topointer(L, index));
60 case LUA_TLIGHTUSERDATA:
62 sprintf(buff, "userdata(%p)", lua_touserdata(L, index));
66 sprintf(buff, "thread");
73 * @brief Returns a string representation of a key-value pair.
75 * This function is for debugging purposes.
76 * It always returns the same pointer.
78 * @param L the Lua state
79 * @param key_index index of the key
80 * @param value_index index of the value
81 * @return a string representation of the key-value pair
83 const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) {
86 /* value_tostring also always returns the same pointer */
87 int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index));
88 snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index));
93 * @brief Returns a string composed of the specified number of spaces.
95 * This function can be used to indent strings for debugging purposes.
96 * It always returns the same pointer.
98 * @param length length of the string
99 * @return a string of this length with only spaces
101 const char* sglua_get_spaces(int length) {
103 static char spaces[128];
105 xbt_assert(length < 128);
107 memset(spaces, ' ', length);
109 spaces[length] = '\0';
114 * @brief Dumps the Lua stack if debug logs are enabled.
115 * @param msg a message to print
116 * @param L a Lua state
118 void sglua_stack_dump(const char* msg, lua_State* L)
120 if (XBT_LOG_ISENABLED(lua_utils, xbt_log_priority_debug)) {
124 int top = lua_gettop(L);
131 for (i = 1; i <= top; i++) { /* repeat for each level */
133 p += sprintf(p, "%s", sglua_tostring(L, i));
134 p += sprintf(p, " "); /* put a separator */
136 XBT_DEBUG("%s%s", msg, buff);
141 * @brief Writes the specified data into a memory buffer.
143 * This function is a valid lua_Writer that writes into a memory buffer passed
146 * @param L a lua state
147 * @param source some data
148 * @param sz number of bytes of data
149 * @param user_data the memory buffer to write
151 int sglua_memory_writer(lua_State* L, const void* source, size_t size,
154 sglua_buffer_t buffer = (sglua_buffer_t) userdata;
155 while (buffer->capacity < buffer->size + size) {
156 buffer->capacity *= 2;
157 buffer->data = xbt_realloc(buffer->data, buffer->capacity);
159 memcpy(buffer->data + buffer->size, source, size);
160 buffer->size += size;