1 /* Copyright (c) 2010-2021. 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 bindings */
9 #include "lua_private.hpp"
10 #include "lua_utils.hpp"
11 #include "src/surf/xml/platf.hpp"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(lua, "Lua Bindings");
16 extern "C" int luaopen_simgrid(lua_State* L);
18 /* ********************************************************************************* */
20 /* ********************************************************************************* */
23 * @brief Prints a log string with debug level.
24 * @param L a Lua state
25 * @return number of values returned to Lua
27 * - Argument 1 (string): the text to print
29 static int debug(lua_State* L)
31 const char* str = luaL_checkstring(L, 1);
37 * @brief Prints a log string with info level.
38 * @param L a Lua state
39 * @return number of values returned to Lua
41 * - Argument 1 (string): the text to print
43 static int info(lua_State* L)
45 const char* str = luaL_checkstring(L, 1);
50 static int error(lua_State* L)
52 const char* str = luaL_checkstring(L, 1);
57 static int critical(lua_State* L)
59 const char* str = luaL_checkstring(L, 1);
60 XBT_CRITICAL("%s", str);
65 * @brief Dumps a lua table with XBT_DEBUG
67 * This function can be called from within lua via "simgrid.dump(table)". It will
68 * then dump the table via XBT_DEBUG
70 static int dump(lua_State* L)
72 int argc = lua_gettop(L);
74 for (int i = 1; i <= argc; i++) {
75 if (lua_istable(L, i)) {
76 lua_pushnil(L); /* table nil */
78 //lua_next pops the topmost element from the stack and
79 //gets the next pair from the table at the specified index
80 while (lua_next(L, i)) { /* table key val */
81 // we need to copy here, as a cast from "Number" to "String"
82 // could happen in Lua.
83 // see remark in the lua manual, function "lua_tolstring"
84 // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
86 lua_pushvalue(L, -2); /* table key val key */
88 XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2).c_str());
91 lua_settop(L, argc); // Remove everything except the initial arguments
98 static const luaL_Reg simgrid_functions[] = {
102 {"critical", critical},
107 /* ********************************************************************************* */
108 /* module management functions */
109 /* ********************************************************************************* */
112 * @brief Makes the core functions available to the Lua world.
113 * @param L a Lua world
115 static void sglua_register_core_functions(lua_State *L)
117 /* register the core C functions to lua */
118 luaL_newlib(L, simgrid_functions); /* simgrid */
119 lua_pushvalue(L, -1); /* simgrid simgrid */
120 lua_setglobal(L, "simgrid"); /* simgrid */
124 * @brief Opens the simgrid Lua module.
126 * This function is called automatically by the Lua interpreter when some
127 * Lua code requires the "simgrid" module.
129 * @param L the Lua state
131 int luaopen_simgrid(lua_State *L)
133 XBT_DEBUG("luaopen_simgrid *****");
135 sglua_register_core_functions(L);
136 sglua_register_host_functions(L);
137 sglua_register_platf_functions(L);