-#define LINK_MODULE_NAME "simgrid.Link"
-#define ROUTE_MODULE_NAME "simgrid.Route"
-#define AS_MODULE_NAME "simgrid.AS"
-#define TRACE_MODULE_NAME "simgrid.Trace"
-
-/**
- * @brief A chunk of memory.
- *
- * TODO replace this by a dynar
- */
-typedef struct s_buffer {
- char* data;
- size_t size;
- size_t capacity;
-} s_buffer_t, *buffer_t;
-
-static const char* value_tostring(lua_State* L, int index);
-static const char* keyvalue_tostring(lua_State* L, int key_index, int value_index);
-static void stack_dump(const char *msg, lua_State* L);
-static int writer(lua_State* L, const void* source, size_t size, void* userdata);
-static void move_value(lua_State* src, lua_State* dst);
-static void move_value_impl(lua_State* src, lua_State* dst, const char* name);
-static int l_get_from_father(lua_State* L);
-static lua_State *clone_lua_state(lua_State* L);
-static m_task_t check_task(lua_State *L, int index);
-static void register_c_functions(lua_State *L);
-
-/* ********************************************************************************* */
-/* helper functions */
-/* ********************************************************************************* */
-
-/**
- * @brief Returns a string representation of a value in the Lua stack.
- *
- * This function is for debugging purposes.
- * It always returns the same pointer.
- *
- * @param L the Lua state
- * @param index index in the stack
- * @return a string representation of the value at this index
- */
-static const char* value_tostring(lua_State* L, int index) {
-
- static char buff[64];
-
- switch (lua_type(L, index)) {
-
- case LUA_TNIL:
- sprintf(buff, "nil");
- break;
-
- case LUA_TNUMBER:
- sprintf(buff, "%.3f", lua_tonumber(L, index));
- break;
-
- case LUA_TBOOLEAN:
- sprintf(buff, "%s", lua_toboolean(L, index) ? "true" : "false");
- break;
-
- case LUA_TSTRING:
- snprintf(buff, 63, "'%s'", lua_tostring(L, index));
- break;
-
- case LUA_TFUNCTION:
- if (lua_iscfunction(L, index)) {
- sprintf(buff, "C-function");
- }
- else {
- sprintf(buff, "function");
- }
- break;
-
- case LUA_TTABLE:
- sprintf(buff, "table(%zu)", lua_objlen(L, index));
- break;
-
- case LUA_TLIGHTUSERDATA:
- case LUA_TUSERDATA:
- sprintf(buff, "userdata(%p)", lua_touserdata(L, index));
- break;
-
- case LUA_TTHREAD:
- sprintf(buff, "thread");
- break;
- }
- return buff;
-}
-
-/**
- * @brief Returns a string representation of a key-value pair.
- *
- * This function is for debugging purposes.
- * It always returns the same pointer.
- *
- * @param L the Lua state
- * @param key_index index of the key
- * @param value_index index of the value
- * @return a string representation of the key-value pair
- */
-static const char* keyvalue_tostring(lua_State* L, int key_index, int value_index) {
-
- static char buff[64];
- /* value_tostring also always returns the same pointer */
- int len = snprintf(buff, 63, "[%s] -> ", value_tostring(L, key_index));
- snprintf(buff + len, 63 - len, "%s", value_tostring(L, value_index));
- return buff;
-}
-
-/**
- * @brief Returns a string composed of the specified number of spaces.
- *
- * This function is for debugging purposes.
- * It always returns the same pointer.
- *
- * @param length length of the string
- * @return a string of this length with only spaces
- */
-static const char* get_spaces(int length) {
-
- static char spaces[128];
-
- xbt_assert(length < 128);
- memset(spaces, ' ', length);
- spaces[length] = '\0';
- return spaces;
-}
-
-/**
- * @brief Dumps the Lua stack if debug logs are enabled.
- * @param msg a message to print
- * @param L a Lua state
- */
-static void stack_dump(const char* msg, lua_State* L)
-{
- if (XBT_LOG_ISENABLED(lua, xbt_log_priority_debug)) {
- char buff[2048];
- char* p = buff;
- int i;
- int top = lua_gettop(L);
-
- //if (1) return;
-
- fflush(stdout);
-
- p[0] = '\0';
- for (i = 1; i <= top; i++) { /* repeat for each level */
-
- p += sprintf(p, "%s", value_tostring(L, i));
- p += sprintf(p, " "); /* put a separator */
- }
- XBT_DEBUG("%s%s", msg, buff);
- }
-}
-
-/**
- * @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.
- *
- * @param L a lua state
- * @param source some data
- * @param sz number of bytes of data
- * @param user_data the memory buffer to write
- */
-static int writer(lua_State* L, const void* source, size_t size, void* userdata) {
-
- buffer_t buffer = (buffer_t) userdata;
- while (buffer->capacity < buffer->size + size) {
- buffer->capacity *= 2;
- buffer->data = xbt_realloc(buffer->data, buffer->capacity);
- }
- memcpy(buffer->data + buffer->size, source, size);
- buffer->size += size;
-
- return 0;
-}
-
-/**
- * @brief Pops a value from a state and pushes it onto the stack of another
- * state.
- *
- * @param src the source state
- * @param dst the destination state
- */
-static void move_value(lua_State* src, lua_State* dst) {
-
- if (src != dst) {
-
- /* create a list of visited tables at index 1 of dst */
- /* src: ... value
- dst: ... */
- lua_newtable(dst);
- /* dst: ... visited */
- lua_insert(dst, 1);
- /* dst: visited ... */
-
- move_value_impl(src, dst, value_tostring(src, -1));
- /* src: ...
- dst: visited ... value */
- lua_remove(dst, 1);
- /* dst: ... value */
- stack_dump("src after xmove: ", src);
- stack_dump("dst after xmove: ", dst);
- }
-}
-
-/**
- * @brief Pops a value from the stack of a source state and pushes it on the
- * stack of another state.
- *
- * If the value is a table, its content is copied recursively. To avoid cycles,
- * a table of previously visited tables must be present at index 1 of dst.
- * Its keys are pointers to visited tables in src and its values are the tables
- * already built.
- *
- * TODO: add support of closures
- *
- * @param src the source state
- * @param dst the destination state, with a list of visited tables at index 1
- * @param name a name describing the value
- */
-static void move_value_impl(lua_State* src, lua_State *dst, const char* name) {
-
- luaL_checkany(src, -1); /* check the value to copy */
- luaL_checktype(dst, 1, LUA_TTABLE); /* check the presence of a table of
- previously visited tables */
-
- int indent = (lua_gettop(dst) - 1) * 6;
- XBT_DEBUG("%sCopying data %s", get_spaces(indent), name);
- indent += 2;
-
- stack_dump("src before copying a value (should be ... value): ", src);
- stack_dump("dst before copying a value (should be visited ...): ", dst);
-
- switch (lua_type(src, -1)) {
-
- case LUA_TNIL:
- lua_pushnil(dst);
- break;
-
- case LUA_TNUMBER:
- lua_pushnumber(dst, lua_tonumber(src, -1));
- break;
-
- case LUA_TBOOLEAN:
- lua_pushboolean(dst, lua_toboolean(src, -1));
- break;
-
- case LUA_TSTRING:
- /* no worries about memory: lua_pushstring makes a copy */
- lua_pushstring(dst, lua_tostring(src, -1));
- break;