Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sonar is picky, so let's be verbose
[simgrid.git] / src / bindings / lua / simgrid_lua.cpp
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include "lua_utils.h"
11 #include "src/surf/xml/platf.hpp"
12
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(lua, "Lua Bindings");
15
16 extern "C" {
17 #include <lauxlib.h>
18
19 int luaopen_simgrid(lua_State *L);
20 }
21
22 /* ********************************************************************************* */
23 /*                                  simgrid API                                      */
24 /* ********************************************************************************* */
25
26 /**
27  * \brief Prints a log string with debug level.
28  * \param L a Lua state
29  * \return number of values returned to Lua
30  *
31  * - Argument 1 (string): the text to print
32  */
33 static int debug(lua_State* L) {
34
35   const char* str = luaL_checkstring(L, 1);
36   XBT_DEBUG("%s", str);
37   return 0;
38 }
39
40 /**
41  * \brief Prints a log string with info level.
42  * \param L a Lua state
43  * \return number of values returned to Lua
44  *
45  * - Argument 1 (string): the text to print
46  */
47 static int info(lua_State* L) {
48
49   const char* str = luaL_checkstring(L, 1);
50   XBT_INFO("%s", str);
51   return 0;
52 }
53
54 static int error(lua_State* L) {
55
56   const char* str = luaL_checkstring(L, 1);
57   XBT_ERROR("%s", str);
58   return 0;
59 }
60
61 static int critical(lua_State* L) {
62
63   const char* str = luaL_checkstring(L, 1);
64   XBT_CRITICAL("%s", str);
65   return 0;
66 }
67
68 /**
69  * @brief Dumps a lua table with XBT_DEBUG
70  *
71  * This function can be called from within lua via "simgrid.dump(table)". It will
72  * then dump the table via XBT_DEBUG 
73  */
74 static int dump(lua_State* L) {
75   int argc = lua_gettop(L);
76
77   for (int i = 1; i <= argc; i++) {
78     if (lua_istable(L, i)) {
79       lua_pushnil(L); /* table nil */
80
81       //lua_next pops the topmost element from the stack and 
82       //gets the next pair from the table at the specified index
83       while (lua_next(L, i)) { /* table key val  */
84         // we need to copy here, as a cast from "Number" to "String"
85         // could happen in Lua.
86         // see remark in the lua manual, function "lua_tolstring"
87         // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
88
89         lua_pushvalue(L, -2); /* table key val key */
90
91         XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2));
92       }
93
94       lua_settop(L, argc); // Remove everything except the initial arguments
95     }
96   }
97
98   return 0;
99 }
100
101 static const luaL_Reg simgrid_functions[] = {
102   {"dump", dump},
103   {"debug", debug},
104   {"info", info},
105   {"critical", critical},
106   {"error", error},
107   {nullptr, nullptr}
108 };
109
110 /* ********************************************************************************* */
111 /*                           module management functions                             */
112 /* ********************************************************************************* */
113
114 /**
115  * \brief Makes the core functions available to the Lua world.
116  * \param L a Lua world
117  */
118 static void sglua_register_core_functions(lua_State *L)
119 {
120   /* register the core C functions to lua */
121   luaL_newlib(L, simgrid_functions); /* simgrid */
122   lua_pushvalue(L, -1);              /* simgrid simgrid */
123   lua_setglobal(L, "simgrid");       /* simgrid */
124 }
125
126 /**
127  * \brief Opens the simgrid Lua module.
128  *
129  * This function is called automatically by the Lua interpreter when some
130  * Lua code requires the "simgrid" module.
131  *
132  * \param L the Lua state
133  */
134 int luaopen_simgrid(lua_State *L)
135 {
136   XBT_DEBUG("luaopen_simgrid *****");
137
138   sglua_register_core_functions(L);
139   sglua_register_host_functions(L);
140   sglua_register_platf_functions(L);
141
142   return 1;
143 }