XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
-static lua_State* sglua_maestro_state;
-
int luaopen_simgrid(lua_State *L);
static void sglua_register_c_functions(lua_State *L);
return 0;
}
+/**
+ * @brief Dumps a lua table with XBT_DEBUG
+ *
+ * This function can be called from within lua via "simgrid.dump(table)". It will
+ * then dump the table via XBT_DEBUG
+ */
+static int dump(lua_State* L) {
+ int argc = lua_gettop(L);
+
+ for (int i = 1; i <= argc; i++) {
+ if (lua_istable(L, i)) {
+ lua_pushnil(L); /* table nil */
+
+ //lua_next pops the topmost element from the stack and
+ //gets the next pair from the table at the specified index
+ while (lua_next(L, i)) { /* table key val */
+ // we need to copy here, as a cast from "Number" to "String"
+ // could happen in Lua.
+ // see remark in the lua manual, function "lua_tolstring"
+ // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
+
+ lua_pushvalue(L, -2); /* table key val key */
+
+ const char *key = lua_tostring(L, -1); /* table key val key */
+ const char *val = lua_tostring(L, -2); /* table key val key */
+
+ XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2));
+ }
+
+ lua_settop(L, argc); // Remove everything except the initial arguments
+ }
+ }
+
+ return 0;
+}
+
static const luaL_Reg simgrid_functions[] = {
+ {"dump", dump},
{"debug", debug},
{"info", info},
{"critical", critical},
{
XBT_DEBUG("luaopen_simgrid *****");
- /* Get the command line arguments from the lua interpreter */
- char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
- int argc = 1;
- argv[0] = (char *) "/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */
-
- lua_getglobal(L, "arg");
- /* if arg is a null value, it means we use lua only as a script to init platform
- * else it should be a table and then take arg in consideration
- */
- if (lua_istable(L, -1)) {
- int done = 0;
- while (!done) {
- argc++;
- lua_pushinteger(L, argc - 2);
- lua_gettable(L, -2);
- if (lua_isnil(L, -1)) {
- done = 1;
- } else {
- xbt_assert(lua_isstring(L, -1),
- "argv[%d] got from lua is no string", argc - 1);
- xbt_assert(argc < LUA_MAX_ARGS_COUNT,
- "Too many arguments, please increase LUA_MAX_ARGS_COUNT in %s before recompiling SimGrid if you insist on having more than %d args on command line",
- __FILE__, LUA_MAX_ARGS_COUNT - 1);
- argv[argc - 1] = (char *) luaL_checkstring(L, -1);
- lua_pop(L, 1);
- XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
- }
- }
- argv[argc--] = NULL;
-
- /* Initialize the MSG core */
- XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
- }
- MSG_init(&argc, argv);
- MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
-
- /* Keep the context mechanism informed of our lua world today */
- sglua_maestro_state = L;
-
- /* initialize access to my tables by children Lua states */
- lua_newtable(L);
- lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
-
sglua_register_c_functions(L);
return 1;