XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_state_cloner, bindings, "Lua state management");
static void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr);
+static void sglua_remove_maestro_table(lua_State* L, int index, void* maestro_table_ptr);
static void* sglua_get_maestro_table_ptr(lua_State* L, int index);
static void sglua_get_table_by_ptr(lua_State* L, void* table_ptr);
static int l_get_from_maestro(lua_State* L);
* @param index index of the copy of the maestro table in the stack of L
* @param maestro_table_ptr pointer to the original table in maestro's world
*/
-void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
+static void sglua_add_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
/* we will set both [ptr] -> table and [table] -> ptr */
/* ... */
}
+/**
+ * @brief Removes a reference to a maestro table to the list of known maestro
+ * tables of a state.
+ *
+ * @param L a state (can be maestro itself)
+ * @param index index of the copy of the maestro table in the stack of L
+ * @param maestro_table_ptr pointer to the original table in maestro's world
+ */
+static void sglua_remove_maestro_table(lua_State* L, int index, void* maestro_table_ptr) {
+
+ /* we will unset both [ptr] -> table and [table] -> ptr */
+
+ /* ... */
+ lua_pushvalue(L, index);
+ /* ... table */
+ lua_pushstring(L, "simgrid.maestro_tables");
+ /* ... table "simgrid.maestro_tables" */
+ lua_rawget(L, LUA_REGISTRYINDEX);
+ /* ... table maestrotbs */
+ lua_pushvalue(L, -2);
+ /* ... table maestrotbs table */
+ lua_pushnil(L);
+ /* ... table maestrotbs table nil */
+ lua_pushlightuserdata(L, maestro_table_ptr);
+ /* ... table maestrotbs table nil tableptr */
+ lua_pushnil(L);
+ /* ... table maestrotbs table nil tableptr nil*/
+ lua_settable(L, -5);
+ /* ... table maestrotbs table nil */
+ lua_settable(L, -3);
+ /* ... table maestrotbs */
+ lua_pop(L, 2);
+ /* ... */
+}
+
/**
* @brief For a table in the stack of L, returns a pointer that identifies the
* same table in in maestro's world.
* @param src the source state (not necessarily maestro)
* @param dst the destination state
*/
-void sglua_move_value(lua_State* src, lua_State *dst) {
+void sglua_move_value(lua_State* src, lua_State* dst) {
+
+ sglua_copy_value(src, dst);
+ lua_pop(src, 1);
+}
+
+/**
+ * @brief Pushes onto the stack a copy of the value on top another stack.
+ * If the value is a table, its content is copied recursively.
+ *
+ * This function allows to move a value between two different global states.
+ *
+ * @param src the source state (not necessarily maestro)
+ * @param dst the destination state
+ */
+void sglua_copy_value(lua_State* src, lua_State* dst) {
luaL_checkany(src, -1); /* check the value to copy */
break;
}
- /* the value has been copied to dst: remove it from src */
- lua_pop(src, 1);
-
- indent -= 2;
XBT_DEBUG("%sData copied", sglua_get_spaces(indent));
- sglua_stack_dump("src after copying a value (should be ...): ", src);
+ sglua_stack_dump("src after copying a value (should be ... value): ", src);
sglua_stack_dump("dst after copying a value (should be ... value): ", dst);
}
}
/**
- * @brief Copies the table value on the top of src to the top of dst.
+ * @brief Copies the table value on top of src to the top of dst.
*
* A deep copy of the table is made. If the table has a metatable, the
* metatable is also copied.
- * If the table is already known by the destination state, it is not copied
- * again.
+ * If the table comes from maestro and is already known by the destination
+ * state, it is not copied again.
*
* @param src source state
* @param dst destination state
/* get from maestro the pointer that identifies this table */
void* table_ptr = sglua_get_maestro_table_ptr(src, -1);
- if (table_ptr == NULL) {
+ int known_by_maestro = (table_ptr != NULL);
+
+ if (!known_by_maestro) {
/* the table didn't come from maestro: nevermind, use the pointer of src */
table_ptr = (void*) lua_topointer(src, -1);
-
- if (!sglua_is_maestro(src)) {
- XBT_DEBUG("%sMaestro does not know this table",
- sglua_get_spaces(indent));
- }
+ XBT_DEBUG("%sMaestro does not know this table",
+ sglua_get_spaces(indent));
}
if (sglua_is_maestro(src)) {
XBT_DEBUG("%sKeeping track of this table in maestro itself",
sglua_get_spaces(indent));
sglua_add_maestro_table(src, -1, table_ptr);
+ known_by_maestro = 1;
xbt_assert(sglua_get_maestro_table_ptr(src, -1) == table_ptr);
}
/* mark the table as known right now to avoid infinite recursion */
sglua_add_maestro_table(dst, -1, table_ptr);
- /* FIXME: we may have added a table with a non-maestro pointer, is this a
- problem? */
+ /* we may have added a table with a non-maestro pointer, but if it was the
+ * case, we will remove it later */
XBT_DEBUG("%sTable marked as known", sglua_get_spaces(indent));
xbt_assert(sglua_get_maestro_table_ptr(dst, -1) == table_ptr);
}
XBT_DEBUG("%sFinished traversing the table", sglua_get_spaces(indent));
}
+
+ if (!known_by_maestro) {
+ /* actually,it was not a maestro table: forget the pointer */
+ sglua_remove_maestro_table(dst, -1, table_ptr);
+ }
}
/**
* @brief Copies the function on the top of src to the top of dst.
*
* It can be a Lua function or a C function.
- * Copying upvalues is not implemented yet.
*
* @param src source state
* @param dst destination state
/* create the table of known tables from maestro */
lua_pushstring(L, "simgrid.maestro_tables");
/* "simgrid.maestro_tables" */
- lua_newtable(L); /* "simgrid.maestro_tables" maestrotbs*/
+ lua_newtable(L); /* "simgrid.maestro_tables" maestrotbs */
lua_rawset(L, LUA_REGISTRYINDEX);
/* -- */