Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix leak of memory caused to some Events
[simgrid.git] / src / bindings / lua / lua_host.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* SimGrid Lua bindings                                                     */
7
8 #include "lua_private.h"
9 #include "simgrid/s4u/Host.hpp"
10 extern "C" {
11 #include <lauxlib.h>
12 }
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(lua_host, "Lua Host module");
15
16 #define HOST_MODULE_NAME "simgrid.host"
17 #define HOST_FIELDNAME   "__simgrid_host"
18 /* ********************************************************************************* */
19 /*                                simgrid.host API                                   */
20 /* ********************************************************************************* */
21
22 /** @brief Ensures that the pointed stack value is an host userdatum and returns it.
23  *
24  * \param L a Lua state
25  * \param index an index in the Lua stack
26  * \return the C host corresponding to this Lua host
27  */
28 sg_host_t sglua_check_host(lua_State * L, int index)
29 {
30   luaL_checktype(L, index, LUA_TTABLE);
31   lua_getfield(L, index, HOST_FIELDNAME);
32   sg_host_t *pi = (sg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
33   lua_pop(L, 1);
34   if (pi == nullptr)
35     XBT_ERROR("luaL_checkudata() returned nullptr");
36   sg_host_t ht = *pi;
37   if (!ht)
38     luaL_error(L, "null Host");
39   return ht;
40 }
41
42 /**
43  * \brief Returns a host given its name. This is a lua function.
44  *
45  * \param L a Lua state
46  * \return number of values returned to Lua
47  *
48  * - Argument 1 (string): name of a host
49  * - Return value (host): the corresponding host will be pushed onto the stack
50  */
51 static int l_host_get_by_name(lua_State * L)
52 {
53   const char *name = luaL_checkstring(L, 1);
54   lua_remove(L, 1); /* remove the args from the stack */
55
56   sg_host_t host = sg_host_by_name(name);
57   lua_ensure(host, "No host name '%s' found.", name);
58
59   lua_newtable(L);                        /* table */
60   sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t)); /* table userdatum */
61   *lua_host = host;
62   luaL_getmetatable(L, HOST_MODULE_NAME); /* table userdatum metatable */
63   lua_setmetatable(L, -2);                /* table userdatum */
64   lua_setfield(L, -2, HOST_FIELDNAME);    /* table -- put the userdata as field of the table */
65
66   return 1;
67 }
68
69 /**
70  * \brief Returns the name of a host.
71  * \param L a Lua state
72  * \return number of values returned to Lua
73  *
74  * - Argument 1 (host): a host
75  * - Return value (string): name of this host
76  */
77 static int l_host_get_name(lua_State * L)
78 {
79   sg_host_t ht = sglua_check_host(L, 1);
80   lua_pushstring(L, ht->cname());
81   return 1;
82 }
83
84 /**
85  * \brief Returns the number of existing hosts.
86  * \param L a Lua state
87  * \return number of values returned to Lua
88  *
89  * - Return value (number): number of hosts
90  */
91 static int l_host_number(lua_State * L)
92 {
93   xbt_dynar_t hosts = sg_hosts_as_dynar();
94   lua_pushinteger(L, xbt_dynar_length(hosts));
95   xbt_dynar_free(&hosts);
96   return 1;
97 }
98
99 /**
100  * \brief Returns the host given its index.
101  * \param L a Lua state
102  * \return number of values returned to Lua
103  *
104  * - Argument 1 (number): an index (1 is the first)
105  * - Return value (host): the host at this index
106  */
107 static int l_host_at(lua_State * L)
108 {
109   int index = luaL_checkinteger(L, 1);
110   xbt_dynar_t hosts = sg_hosts_as_dynar();
111   sg_host_t host = xbt_dynar_get_as(hosts,index - 1,sg_host_t);// lua indexing start by 1 (lua[1] <=> C[0])
112   lua_newtable(L);              /* create a table, put the userdata on top of it */
113   sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t));
114   *lua_host = host;
115   luaL_getmetatable(L, HOST_MODULE_NAME);
116   lua_setmetatable(L, -2);
117   lua_setfield(L, -2, HOST_FIELDNAME);        /* put the userdata as field of the table */
118   xbt_dynar_free(&hosts);
119   return 1;
120 }
121
122 /**
123  * \brief Returns the value of a host property.
124  * \param L a Lua state
125  * \return number of values returned to Lua
126  *
127  * - Argument 1 (host): a host
128  * - Argument 2 (string): name of the property to get
129  * - Return value (string): the value of this property
130  */
131 static int l_host_get_property_value(lua_State * L)
132 {
133   sg_host_t ht = sglua_check_host(L, 1);
134   const char *prop = luaL_checkstring(L, 2);
135   lua_pushstring(L, sg_host_get_property_value(ht,prop));
136   return 1;
137 }
138
139 /**
140  * \brief Destroys a host.
141  * \param L a Lua state
142  * \return number of values returned to Lua
143  *
144  * - Argument 1 (host): the host to destroy
145  */
146 static int l_host_destroy(lua_State *L)
147 {
148   //sg_host_t ht = sglua_check_host(L, 1);
149   //FIXME: not working..__MSG_host_priv_free(MSG_host_priv(ht));
150   return 0;
151 }
152
153 static const luaL_Reg host_functions[] = {
154   {"get_by_name", l_host_get_by_name},
155   {"name", l_host_get_name},
156   {"number", l_host_number},
157   {"at", l_host_at},
158   {"get_prop_value", l_host_get_property_value},
159   {"destroy", l_host_destroy},
160   // Bypass XML Methods
161   {"set_property", console_host_set_property},
162   {nullptr, nullptr}
163 };
164
165 /**
166  * \brief Returns a string representation of a host.
167  * \param L a Lua state
168  * \return number of values returned to Lua
169  *
170  * - Argument 1 (userdata): a host
171  * - Return value (string): a string describing this host
172  */
173 static int l_host_tostring(lua_State * L)
174 {
175   lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
176   return 1;
177 }
178
179 static const luaL_Reg host_meta[] = {
180   {"__tostring", l_host_tostring},
181   {0, 0}
182 };
183
184 /**
185  * \brief Registers the host functions into the table simgrid.host.
186  *
187  * Also initialize the metatable of the host userdata type.
188  *
189  * \param L a lua state
190  */
191 void sglua_register_host_functions(lua_State* L)
192 {
193   /* create a table simgrid.host and fill it with host functions */
194   lua_getglobal(L, "simgrid"); /* simgrid */
195   luaL_newlib(L, host_functions); /* simgrid simgrid.host */
196   lua_setfield(L, -2, "host"); /* simgrid */
197   lua_getfield(L, -1, "host");    /* simgrid simgrid.host */
198
199   /* create the metatable for host, add it to the Lua registry */
200   luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid simgrid.host mt */
201
202   /* fill the metatable */
203   luaL_setfuncs(L, host_meta, 0);         /* simgrid simgrid.host mt */
204
205   /**
206    * Copy the table and push it onto the stack.
207    * Required for the lua_setfield call below.
208    */
209   lua_getfield(L, -3, "host");                   /* simgrid simgrid.host mt simgrid.host */
210
211   /* metatable.__index = simgrid.host
212    * we put the host functions inside the host userdata itself:
213    * this allows to write my_host:method(args) for
214    * simgrid.host.method(my_host, args) */
215   lua_setfield(L, -2, "__index");         /* simgrid simgrid.host mt */
216
217   lua_setmetatable(L, -2);                /* simgrid simgrid.host */
218   lua_pop(L, 2);                          /* -- */
219 }
220