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/Host.hpp"
12 constexpr char HOST_MODULE_NAME[] = "simgrid.host";
13 constexpr char HOST_FIELDNAME[] = "__simgrid_host";
15 /* ********************************************************************************* */
16 /* simgrid.host API */
17 /* ********************************************************************************* */
19 /** @brief Ensures that the pointed stack value is a host userdatum and returns it.
21 * @param L a Lua state
22 * @param index an index in the Lua stack
23 * @return the C host corresponding to this Lua host
25 sg_host_t sglua_check_host(lua_State * L, int index)
27 luaL_checktype(L, index, LUA_TTABLE);
28 lua_getfield(L, index, HOST_FIELDNAME);
29 sg_host_t *pi = (sg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
31 xbt_assert(pi != nullptr, "luaL_checkudata() returned nullptr");
34 luaL_error(L, "null Host");
39 * @brief Returns a host given its name. This is a lua function.
41 * @param L a Lua state
42 * @return number of values returned to Lua
44 * - Argument 1 (string): name of a host
45 * - Return value (host): the corresponding host will be pushed onto the stack
47 static int l_host_get_by_name(lua_State * L)
49 const char *name = luaL_checkstring(L, 1);
50 lua_remove(L, 1); /* remove the args from the stack */
52 sg_host_t host = sg_host_by_name(name);
53 lua_ensure(host, "No host name '%s' found.", name);
55 lua_newtable(L); /* table */
56 sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t)); /* table userdatum */
58 luaL_getmetatable(L, HOST_MODULE_NAME); /* table userdatum metatable */
59 lua_setmetatable(L, -2); /* table userdatum */
60 lua_setfield(L, -2, HOST_FIELDNAME); /* table -- put the userdata as field of the table */
66 * @brief Returns the name of a host.
67 * @param L a Lua state
68 * @return number of values returned to Lua
70 * - Argument 1 (host): a host
71 * - Return value (string): name of this host
73 static int l_host_get_name(lua_State * L)
75 auto const* ht = sglua_check_host(L, 1);
76 lua_pushstring(L, ht->get_cname());
81 * @brief Returns the number of existing hosts.
82 * @param L a Lua state
83 * @return number of values returned to Lua
85 * - Return value (number): number of hosts
87 static int l_host_number(lua_State * L)
89 xbt_dynar_t hosts = sg_hosts_as_dynar();
90 lua_pushinteger(L, xbt_dynar_length(hosts));
91 xbt_dynar_free(&hosts);
96 * @brief Returns the host given its index.
97 * @param L a Lua state
98 * @return number of values returned to Lua
100 * - Argument 1 (number): an index (1 is the first)
101 * - Return value (host): the host at this index
103 static int l_host_at(lua_State * L)
105 int index = luaL_checkinteger(L, 1);
106 xbt_dynar_t hosts = sg_hosts_as_dynar();
107 sg_host_t host = xbt_dynar_get_as(hosts,index - 1,sg_host_t);// lua indexing start by 1 (lua[1] <=> C[0])
108 lua_newtable(L); /* create a table, put the userdata on top of it */
109 sg_host_t *lua_host = (sg_host_t *) lua_newuserdata(L, sizeof(sg_host_t));
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 xbt_dynar_free(&hosts);
119 * @brief Returns the value of a host property.
120 * @param L a Lua state
121 * @return number of values returned to Lua
123 * - Argument 1 (host): a host
124 * - Argument 2 (string): name of the property to get
125 * - Return value (string): the value of this property
127 static int l_host_get_property_value(lua_State * L)
129 const_sg_host_t ht = sglua_check_host(L, 1);
130 const char *prop = luaL_checkstring(L, 2);
131 lua_pushstring(L, sg_host_get_property_value(ht,prop));
136 * @brief Destroys a host.
137 * @param L a Lua state
138 * @return number of values returned to Lua
140 * - Argument 1 (host): the host to destroy
142 static int l_host_destroy(lua_State *L)
144 sg_host_t ht = sglua_check_host(L, 1);
149 static const luaL_Reg host_functions[] = {
150 {"get_by_name", l_host_get_by_name},
151 {"name", l_host_get_name},
152 {"number", l_host_number},
154 {"get_prop_value", l_host_get_property_value},
155 {"destroy", l_host_destroy},
156 // Bypass XML Methods
157 {"set_property", console_host_set_property},
162 * @brief Returns a string representation of a host.
163 * @param L a Lua state
164 * @return number of values returned to Lua
166 * - Argument 1 (userdata): a host
167 * - Return value (string): a string describing this host
169 static int l_host_tostring(lua_State * L)
171 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
175 static const luaL_Reg host_meta[] = {
176 {"__tostring", l_host_tostring},
181 * @brief Registers the host functions into the table simgrid.host.
183 * Also initialize the metatable of the host userdata type.
185 * @param L a lua state
187 void sglua_register_host_functions(lua_State* L)
189 /* create a table simgrid.host and fill it with host functions */
190 lua_getglobal(L, "simgrid"); /* simgrid */
191 luaL_newlib(L, host_functions); /* simgrid simgrid.host */
192 lua_setfield(L, -2, "host"); /* simgrid */
193 lua_getfield(L, -1, "host"); /* simgrid simgrid.host */
195 /* create the metatable for host, add it to the Lua registry */
196 luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid simgrid.host mt */
198 /* fill the metatable */
199 luaL_setfuncs(L, host_meta, 0); /* simgrid simgrid.host mt */
202 * Copy the table and push it onto the stack.
203 * Required for the lua_setfield call below.
205 lua_getfield(L, -3, "host"); /* simgrid simgrid.host mt simgrid.host */
207 /* metatable.__index = simgrid.host
208 * we put the host functions inside the host userdata itself:
209 * this allows one to write my_host:method(args) for
210 * simgrid.host.method(my_host, args) */
211 lua_setfield(L, -2, "__index"); /* simgrid simgrid.host mt */
213 lua_setmetatable(L, -2); /* simgrid simgrid.host */
214 lua_pop(L, 2); /* -- */