Logo AND Algorithmique Numérique Distribuée

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