1 /* Copyright (c) 2010-2015. 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.h"
10 #include "lua_utils.h"
12 #include "simgrid/msg.h"
13 #include "simgrid/simdag.h"
14 #include "surf/surfxml_parse.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
22 int luaopen_simgrid(lua_State *L);
23 static void sglua_register_c_functions(lua_State *L);
26 /* ********************************************************************************* */
28 /* ********************************************************************************* */
31 * \brief Prints a log string with debug level.
32 * \param L a Lua state
33 * \return number of values returned to Lua
35 * - Argument 1 (string): the text to print
37 static int debug(lua_State* L) {
39 const char* str = luaL_checkstring(L, 1);
45 * \brief Prints a log string with info level.
46 * \param L a Lua state
47 * \return number of values returned to Lua
49 * - Argument 1 (string): the text to print
51 static int info(lua_State* L) {
53 const char* str = luaL_checkstring(L, 1);
58 static int error(lua_State* L) {
60 const char* str = luaL_checkstring(L, 1);
65 static int critical(lua_State* L) {
67 const char* str = luaL_checkstring(L, 1);
68 XBT_CRITICAL("%s", str);
73 * @brief Dumps a lua table with XBT_DEBUG
75 * This function can be called from within lua via "simgrid.dump(table)". It will
76 * then dump the table via XBT_DEBUG
78 static int dump(lua_State* L) {
79 int argc = lua_gettop(L);
81 for (int i = 1; i <= argc; i++) {
82 if (lua_istable(L, i)) {
83 lua_pushnil(L); /* table nil */
85 //lua_next pops the topmost element from the stack and
86 //gets the next pair from the table at the specified index
87 while (lua_next(L, i)) { /* table key val */
88 // we need to copy here, as a cast from "Number" to "String"
89 // could happen in Lua.
90 // see remark in the lua manual, function "lua_tolstring"
91 // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
93 lua_pushvalue(L, -2); /* table key val key */
95 XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2));
98 lua_settop(L, argc); // Remove everything except the initial arguments
105 static const luaL_Reg simgrid_functions[] = {
109 {"critical", critical},
114 /* ********************************************************************************* */
115 /* module management functions */
116 /* ********************************************************************************* */
119 * \brief Opens the simgrid Lua module.
121 * This function is called automatically by the Lua interpreter when some
122 * Lua code requires the "simgrid" module.
124 * \param L the Lua state
126 int luaopen_simgrid(lua_State *L)
128 XBT_DEBUG("luaopen_simgrid *****");
130 sglua_register_c_functions(L);
137 * \brief Makes the core functions available to the Lua world.
138 * \param L a Lua world
140 static void sglua_register_core_functions(lua_State *L)
142 /* register the core C functions to lua */
143 luaL_newlib(L, simgrid_functions); /* simgrid */
144 lua_pushvalue(L, -1); /* simgrid simgrid */
145 lua_setglobal(L, "simgrid"); /* simgrid */
149 * \brief Creates the simgrid module and make it available to Lua.
150 * \param L a Lua world
152 static void sglua_register_c_functions(lua_State *L)
154 sglua_register_core_functions(L);
155 sglua_register_host_functions(L);
156 sglua_register_platf_functions(L);