Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge conflicts
[simgrid.git] / src / bindings / lua / lua_utils.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* SimGrid Lua helper functions                                             */
8
9 #include <lauxlib.h>
10 #include "lua_utils.h"
11 #include "xbt.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_utils, bindings, "Lua helper functions");
15
16 /**
17  * @brief Returns a string representation of a value in the Lua stack.
18  *
19  * This function is for debugging purposes.
20  * It always returns the same pointer.
21  *
22  * @param L the Lua state
23  * @param index index in the stack
24  * @return a string representation of the value at this index
25  */
26 const char* sglua_tostring(lua_State* L, int index) {
27
28   static char buff[64];
29
30   switch (lua_type(L, index)) {
31
32     case LUA_TNIL:
33       sprintf(buff, "nil");
34       break;
35
36     case LUA_TNUMBER:
37       sprintf(buff, "%.3f", lua_tonumber(L, index));
38       break;
39
40     case LUA_TBOOLEAN:
41       sprintf(buff, "%s", lua_toboolean(L, index) ? "true" : "false");
42       break;
43
44     case LUA_TSTRING:
45       snprintf(buff, 63, "'%s'", lua_tostring(L, index));
46       break;
47
48     case LUA_TFUNCTION:
49       if (lua_iscfunction(L, index)) {
50         sprintf(buff, "C-function");
51       }
52       else {
53         sprintf(buff, "function");
54       }
55       break;
56
57     case LUA_TTABLE:
58       sprintf(buff, "table(%p)", lua_topointer(L, index));
59       break;
60
61     case LUA_TLIGHTUSERDATA:
62     case LUA_TUSERDATA:
63       sprintf(buff, "userdata(%p)", lua_touserdata(L, index));
64       break;
65
66     case LUA_TTHREAD:
67       sprintf(buff, "thread");
68       break;
69   }
70   return buff;
71 }
72
73 /**
74  * @brief Returns a string representation of a key-value pair.
75  *
76  * This function is for debugging purposes.
77  * It always returns the same pointer.
78  *
79  * @param L the Lua state
80  * @param key_index index of the key
81  * @param value_index index of the value
82  * @return a string representation of the key-value pair
83  */
84 const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) {
85
86   static char buff[64];
87   /* value_tostring also always returns the same pointer */
88   int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index));
89   snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index));
90   return buff;
91 }
92
93 /**
94  * @brief Returns a string composed of the specified number of spaces.
95  *
96  * This function can be used to indent strings for debugging purposes.
97  * It always returns the same pointer.
98  *
99  * @param length length of the string
100  * @return a string of this length with only spaces
101  */
102 const char* sglua_get_spaces(int length) {
103
104   static char spaces[128];
105
106   xbt_assert(length < 128);
107   if (length != 0) {
108     memset(spaces, ' ', length);
109   }
110   spaces[length] = '\0';
111   return spaces;
112 }
113
114 /**
115  * @brief Dumps the Lua stack if debug logs are enabled.
116  * @param msg a message to print
117  * @param L a Lua state
118  */
119 void sglua_stack_dump(const char* msg, lua_State* L)
120 {
121   if (XBT_LOG_ISENABLED(lua_utils, xbt_log_priority_debug)) {
122     char buff[2048];
123     char* p = buff;
124     int i;
125     int top = lua_gettop(L);
126
127     //if (1) return;
128
129     fflush(stdout);
130
131     p[0] = '\0';
132     for (i = 1; i <= top; i++) {  /* repeat for each level */
133
134       p += sprintf(p, "%s", sglua_tostring(L, i));
135       p += sprintf(p, " ");       /* put a separator */
136     }
137     XBT_DEBUG("%s%s", msg, buff);
138   }
139 }
140
141 /**
142  * \brief Like luaL_checkudata, with additional debug logs.
143  *
144  * This function is for debugging purposes only.
145  *
146  * \param L a lua state
147  * \param ud index of the userdata to check in the stack
148  * \param tname key of the metatable of this userdata in the registry
149  */
150 void* sglua_checkudata_debug(lua_State* L, int ud, const char* tname)
151 {
152   XBT_DEBUG("Checking the userdata: ud = %d", ud);
153   sglua_stack_dump("my_checkudata: ", L);
154   void* p = lua_touserdata(L, ud);
155   lua_getfield(L, LUA_REGISTRYINDEX, tname);
156   const void* correct_mt = lua_topointer(L, -1);
157
158   int has_mt = lua_getmetatable(L, ud);
159   XBT_DEBUG("Checking the userdata: has metatable ? %d", has_mt);
160   const void* actual_mt = NULL;
161   if (has_mt) {
162     actual_mt = lua_topointer(L, -1);
163     lua_pop(L, 1);
164   }
165   XBT_DEBUG("Checking the task's metatable: expected %p, found %p", correct_mt, actual_mt);
166   sglua_stack_dump("my_checkudata: ", L);
167
168   if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
169     luaL_typerror(L, ud, tname);
170   lua_pop(L, 2);
171   return p;
172 }
173
174 /**
175  * @brief Writes the specified data into a memory buffer.
176  *
177  * This function is a valid lua_Writer that writes into a memory buffer passed
178  * as userdata.
179  *
180  * @param L a lua state
181  * @param source some data
182  * @param sz number of bytes of data
183  * @param user_data the memory buffer to write
184  */
185 int sglua_memory_writer(lua_State* L, const void* source, size_t size,
186     void* userdata) {
187
188   sglua_buffer_t buffer = (sglua_buffer_t) userdata;
189   while (buffer->capacity < buffer->size + size) {
190     buffer->capacity *= 2;
191     buffer->data = xbt_realloc(buffer->data, buffer->capacity);
192   }
193   memcpy(buffer->data + buffer->size, source, size);
194   buffer->size += size;
195
196   return 0;
197 }