Logo AND Algorithmique Numérique Distribuée

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