Logo AND Algorithmique Numérique Distribuée

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