Logo AND Algorithmique Numérique Distribuée

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