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"
11 #include "src/surf/xml/platf.hpp"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(lua, "Lua Bindings");
19 int luaopen_simgrid(lua_State *L);
20 static void sglua_register_c_functions(lua_State *L);
23 /* ********************************************************************************* */
25 /* ********************************************************************************* */
28 * \brief Prints a log string with debug level.
29 * \param L a Lua state
30 * \return number of values returned to Lua
32 * - Argument 1 (string): the text to print
34 static int debug(lua_State* L) {
36 const char* str = luaL_checkstring(L, 1);
42 * \brief Prints a log string with info level.
43 * \param L a Lua state
44 * \return number of values returned to Lua
46 * - Argument 1 (string): the text to print
48 static int info(lua_State* L) {
50 const char* str = luaL_checkstring(L, 1);
55 static int error(lua_State* L) {
57 const char* str = luaL_checkstring(L, 1);
62 static int critical(lua_State* L) {
64 const char* str = luaL_checkstring(L, 1);
65 XBT_CRITICAL("%s", str);
70 * @brief Dumps a lua table with XBT_DEBUG
72 * This function can be called from within lua via "simgrid.dump(table)". It will
73 * then dump the table via XBT_DEBUG
75 static int dump(lua_State* L) {
76 int argc = lua_gettop(L);
78 for (int i = 1; i <= argc; i++) {
79 if (lua_istable(L, i)) {
80 lua_pushnil(L); /* table nil */
82 //lua_next pops the topmost element from the stack and
83 //gets the next pair from the table at the specified index
84 while (lua_next(L, i)) { /* table key val */
85 // we need to copy here, as a cast from "Number" to "String"
86 // could happen in Lua.
87 // see remark in the lua manual, function "lua_tolstring"
88 // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
90 lua_pushvalue(L, -2); /* table key val key */
92 XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2));
95 lua_settop(L, argc); // Remove everything except the initial arguments
102 static const luaL_Reg simgrid_functions[] = {
106 {"critical", critical},
111 /* ********************************************************************************* */
112 /* module management functions */
113 /* ********************************************************************************* */
116 * \brief Opens the simgrid Lua module.
118 * This function is called automatically by the Lua interpreter when some
119 * Lua code requires the "simgrid" module.
121 * \param L the Lua state
123 int luaopen_simgrid(lua_State *L)
125 XBT_DEBUG("luaopen_simgrid *****");
127 sglua_register_c_functions(L);
134 * \brief Makes the core functions available to the Lua world.
135 * \param L a Lua world
137 static void sglua_register_core_functions(lua_State *L)
139 /* register the core C functions to lua */
140 luaL_newlib(L, simgrid_functions); /* simgrid */
141 lua_pushvalue(L, -1); /* simgrid simgrid */
142 lua_setglobal(L, "simgrid"); /* simgrid */
146 * \brief Creates the simgrid module and make it available to Lua.
147 * \param L a Lua world
149 static void sglua_register_c_functions(lua_State *L)
151 sglua_register_core_functions(L);
152 sglua_register_host_functions(L);
153 sglua_register_platf_functions(L);