Logo AND Algorithmique Numérique Distribuée

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