1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved. */
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. */
6 /* SimGrid Lua bindings */
8 #include "lua_private.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "simgrid/s4u/Host.hpp"
13 constexpr char HOST_MODULE_NAME[] = "simgrid.host";
14 constexpr char HOST_FIELDNAME[] = "__simgrid_host";
16 /* ********************************************************************************* */
17 /* simgrid.host API */
18 /* ********************************************************************************* */
20 /** @brief Ensures that the pointed stack value is a host userdatum and returns it.
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
26 sg_host_t sglua_check_host(lua_State * L, int index)
28 luaL_checktype(L, index, LUA_TTABLE);
29 lua_getfield(L, index, HOST_FIELDNAME);
30 auto* pi = static_cast<sg_host_t*>(luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME));
32 xbt_assert(pi != nullptr, "luaL_checkudata() returned nullptr");
35 luaL_error(L, "null Host");
40 * @brief Returns a host given its name. This is a lua function.
42 * @param L a Lua state
43 * @return number of values returned to Lua
45 * - Argument 1 (string): name of a host
46 * - Return value (host): the corresponding host will be pushed onto the stack
48 static int l_host_get_by_name(lua_State * L)
50 const char *name = luaL_checkstring(L, 1);
51 lua_remove(L, 1); /* remove the args from the stack */
53 sg_host_t host = sg_host_by_name(name);
54 lua_ensure(host, "No host name '%s' found.", name);
56 lua_newtable(L); /* table */
57 auto* lua_host = static_cast<sg_host_t*>(lua_newuserdata(L, sizeof(sg_host_t))); /* table userdatum */
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 */
67 * @brief Returns the name of a host.
68 * @param L a Lua state
69 * @return number of values returned to Lua
71 * - Argument 1 (host): a host
72 * - Return value (string): name of this host
74 static int l_host_get_name(lua_State * L)
76 auto const* ht = sglua_check_host(L, 1);
77 lua_pushstring(L, ht->get_cname());
82 * @brief Returns the number of existing hosts.
83 * @param L a Lua state
84 * @return number of values returned to Lua
86 * - Return value (number): number of hosts
88 static int l_host_number(lua_State * L)
90 lua_pushinteger(L, simgrid::s4u::Engine::get_instance()->get_host_count());
95 * @brief Returns the host given its index.
96 * @param L a Lua state
97 * @return number of values returned to Lua
99 * - Argument 1 (number): an index (1 is the first)
100 * - Return value (host): the host at this index
102 static int l_host_at(lua_State * L)
104 lua_Integer 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 auto* lua_host = static_cast<sg_host_t*>(lua_newuserdata(L, sizeof(sg_host_t)));
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 */
117 * @brief Returns the value of a host property.
118 * @param L a Lua state
119 * @return number of values returned to Lua
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
125 static int l_host_get_property_value(lua_State * L)
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));
134 * @brief Destroys a host.
135 * @param L a Lua state
136 * @return number of values returned to Lua
138 * - Argument 1 (host): the host to destroy
140 static int l_host_destroy(lua_State *L)
142 sg_host_t ht = sglua_check_host(L, 1);
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},
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},
160 * @brief Returns a string representation of a host.
161 * @param L a Lua state
162 * @return number of values returned to Lua
164 * - Argument 1 (userdata): a host
165 * - Return value (string): a string describing this host
167 static int l_host_tostring(lua_State * L)
169 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
173 static const luaL_Reg host_meta[] = {{"__tostring", l_host_tostring}, {nullptr, nullptr}};
176 * @brief Registers the host functions into the table simgrid.host.
178 * Also initialize the metatable of the host userdata type.
180 * @param L a lua state
182 void sglua_register_host_functions(lua_State* L)
184 /* create a table simgrid.host and fill it with host functions */
185 lua_getglobal(L, "simgrid"); /* simgrid */
186 luaL_newlib(L, host_functions); /* simgrid simgrid.host */
187 lua_setfield(L, -2, "host"); /* simgrid */
188 lua_getfield(L, -1, "host"); /* simgrid simgrid.host */
190 /* create the metatable for host, add it to the Lua registry */
191 luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid simgrid.host mt */
193 /* fill the metatable */
194 luaL_setfuncs(L, host_meta, 0); /* simgrid simgrid.host mt */
197 * Copy the table and push it onto the stack.
198 * Required for the lua_setfield call below.
200 lua_getfield(L, -3, "host"); /* simgrid simgrid.host mt simgrid.host */
202 /* metatable.__index = simgrid.host
203 * we put the host functions inside the host userdata itself:
204 * this allows one to write my_host:method(args) for
205 * simgrid.host.method(my_host, args) */
206 lua_setfield(L, -2, "__index"); /* simgrid simgrid.host mt */
208 lua_setmetatable(L, -2); /* simgrid simgrid.host */
209 lua_pop(L, 2); /* -- */