1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* SimGrid Lua bindings */
9 #include "simgrid_lua.h"
10 #include "lua_state_cloner.h"
11 #include "lua_utils.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
15 #define TASK_MODULE_NAME "simgrid.task"
16 #define HOST_MODULE_NAME "simgrid.host"
18 #define LINK_MODULE_NAME "simgrid.link"
19 #define ROUTE_MODULE_NAME "simgrid.route"
20 #define PLATF_MODULE_NAME "simgrid.platf"
22 static lua_State* sglua_maestro_state;
24 int luaopen_simgrid(lua_State *L);
25 static void register_c_functions(lua_State *L);
26 static int run_lua_code(int argc, char **argv);
28 /* ********************************************************************************* */
29 /* simgrid.task API */
30 /* ********************************************************************************* */
33 * \brief Ensures that a value in the stack is a valid task and returns it.
34 * \param L a Lua state
35 * \param index an index in the Lua stack
36 * \return the C task corresponding to this Lua task
38 static m_task_t sglua_checktask(lua_State* L, int index)
40 sglua_stack_dump("check task: ", L);
41 luaL_checktype(L, index, LUA_TTABLE);
43 lua_getfield(L, index, "__simgrid_task");
44 /* ... task ... ctask */
45 m_task_t task = *((m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
50 luaL_error(L, "This task was sent to someone else, you cannot access it anymore");
57 * \brief Creates a new task and leaves it onto the stack.
58 * \param L a Lua state
59 * \return number of values returned to Lua
61 * - Argument 1 (string): name of the task
62 * - Argument 2 (number): computation size
63 * - Argument 3 (number): communication size
64 * - Return value (task): the task created
66 * A Lua task is a regular table with a full userdata inside, and both share
67 * the same metatable. For the regular table, the metatable allows OO-style
68 * writing such as your_task:send(someone).
69 * For the userdata, the metatable is used to check its type.
71 static int l_task_new(lua_State* L)
73 XBT_DEBUG("Task new");
74 const char* name = luaL_checkstring(L, 1);
75 int comp_size = luaL_checkint(L, 2);
76 int msg_size = luaL_checkint(L, 3);
80 m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
84 luaL_getmetatable(L, TASK_MODULE_NAME);
86 lua_setmetatable(L, -2);
88 m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t));
91 luaL_getmetatable(L, TASK_MODULE_NAME);
93 lua_setmetatable(L, -2);
95 lua_setfield(L, -2, "__simgrid_task");
101 * \brief Returns the name of a task.
102 * \param L a Lua state
103 * \return number of values returned to Lua
105 * - Argument 1 (task): a task
106 * - Return value (string): name of the task
108 static int l_task_get_name(lua_State* L)
110 m_task_t task = sglua_checktask(L, 1);
111 lua_pushstring(L, MSG_task_get_name(task));
116 * \brief Returns the computation duration of a task.
117 * \param L a Lua state
118 * \return number of values returned to Lua
120 * - Argument 1 (task): a task
121 * - Return value (number): computation duration of this task
123 static int l_task_get_computation_duration(lua_State* L)
125 m_task_t task = sglua_checktask(L, 1);
126 lua_pushnumber(L, MSG_task_get_compute_duration(task));
131 * \brief Executes a task.
132 * \param L a Lua state
133 * \return number of values returned to Lua
135 * - Argument 1 (task): the task to execute
136 * - Return value (nil or error): none if the task was successfully executed, or an error
137 * string in case of failure, which may be "task canceled" or "host failure"
139 static int l_task_execute(lua_State* L)
141 m_task_t task = sglua_checktask(L, 1);
142 MSG_error_t res = MSG_task_execute(task);
149 case MSG_TASK_CANCELED:
150 lua_pushliteral(L, "task canceled");
153 case MSG_HOST_FAILURE:
154 lua_pushliteral(L, "host failure");
158 xbt_die("Unexpected result of MSG_task_execute(): %d, please report this bug", res);
163 * \brief Sends a task to a mailbox and waits for its completion.
164 * \param L a Lua state
165 * \return number of values returned to Lua
167 * - Argument 1 (task): the task to send
168 * - Argument 2 (string or compatible): mailbox name, as a real string or any
169 * type convertible to string (numbers always are)
170 * - Return value (nil or error): none if the communication was successful,
171 * or an error string in case of failure, which may be "timeout",
172 * "host failure" or "transfer failure"
174 static int l_task_send(lua_State* L)
176 m_task_t task = sglua_checktask(L, 1);
177 const char* mailbox = luaL_checkstring(L, 2);
181 /* copy my stack into the task, so that the receiver can copy the lua task */
182 MSG_task_set_data(task, L);
183 MSG_error_t res = MSG_task_send(task, mailbox);
184 while (MSG_task_get_data(task) != NULL) {
185 /* don't mess up with my stack: the receiver didn't copy the data yet */
186 MSG_process_sleep(0);
192 /* the receiver is the owner of the task and may destroy it:
193 * remove the C task on my side so that I don't garbage collect it */
194 lua_getfield(L, 1, "__simgrid_task");
196 m_task_t* udata = (m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME);
201 XBT_DEBUG("MSG_task_send failed: timeout");
203 lua_pushliteral(L, "timeout");
206 case MSG_TRANSFER_FAILURE:
207 XBT_DEBUG("MSG_task_send failed: transfer failure");
209 lua_pushliteral(L, "transfer failure");
212 case MSG_HOST_FAILURE:
213 XBT_DEBUG("MSG_task_send failed: host failure");
215 lua_pushliteral(L, "host failure");
219 xbt_die("Unexpected result of MSG_task_send: %d, please report this bug", res);
224 * \brief Receives a task.
225 * \param L a Lua state
226 * \return number of values returned to Lua
228 * - Argument 1 (string or compatible): mailbox name, as a real string or any
229 * type convertible to string (numbers always are)
230 * - Argument 2 (number, optional): timeout (default is no timeout)
231 * - Return value (task or nil+err): the task received, or nil plus an error message if
232 * the communication has failed
234 static int l_task_recv(lua_State* L)
236 m_task_t task = NULL;
237 const char* mailbox = luaL_checkstring(L, 1);
239 if (lua_gettop(L) >= 2) {
240 /* mailbox timeout */
241 timeout = luaL_checknumber(L, 2);
246 /* no timeout by default */
250 MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
255 /* copy the data directly from sender's stack */
257 lua_State* sender_stack = MSG_task_get_data(task);
258 sglua_copy_value(sender_stack, L);
260 MSG_task_set_data(task, NULL);
265 XBT_DEBUG("MSG_task_send failed: timeout");
267 lua_pushliteral(L, "timeout");
270 case MSG_TRANSFER_FAILURE:
271 XBT_DEBUG("MSG_task_send failed: transfer failure");
273 lua_pushliteral(L, "transfer failure");
276 case MSG_HOST_FAILURE:
277 XBT_DEBUG("MSG_task_send failed: host failure");
279 lua_pushliteral(L, "host failure");
283 xbt_die("Unexpected result of MSG_task_recv: %d, please report this bug", res);
287 static const luaL_reg task_functions[] = {
289 {"get_name", l_task_get_name},
290 {"get_computation_duration", l_task_get_computation_duration},
291 {"execute", l_task_execute},
292 {"send", l_task_send},
293 {"recv", l_task_recv},
298 * \brief Finalizes the userdata of a task.
299 * \param L a Lua state
300 * \return number of values returned to Lua
302 * - Argument 1 (userdata): a C task, possibly NULL if it was sent to another
305 static int l_task_gc(lua_State* L)
308 m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
309 /* the task is NULL if I sent it to someone else */
311 MSG_task_destroy(task);
317 * \brief Returns a string representation of a C task.
318 * \param L a Lua state
319 * \return number of values returned to Lua
321 * - Argument 1 (userdata): a task
322 * - Return value (string): a string describing this task
324 static int l_task_tostring(lua_State* L)
326 m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
327 lua_pushfstring(L, "Task: %p", task);
332 * \brief Metamethods of both a task table and the userdata inside it.
334 static const luaL_reg task_meta[] = {
335 {"__gc", l_task_gc}, /* will be called only for userdata */
336 {"__tostring", l_task_tostring},
340 /* ********************************************************************************* */
341 /* simgrid.host API */
342 /* ********************************************************************************* */
345 * \brief Ensures that a value in the stack is a host and returns it.
346 * \param L a Lua state
347 * \param index an index in the Lua stack
348 * \return the C host corresponding to this Lua host
350 static m_host_t sglua_checkhost(lua_State * L, int index)
353 luaL_checktype(L, index, LUA_TTABLE);
354 lua_getfield(L, index, "__simgrid_host");
355 pi = (m_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
357 luaL_typerror(L, index, HOST_MODULE_NAME);
360 luaL_error(L, "null Host");
366 * \brief Returns a host given its name.
367 * \param L a Lua state
368 * \return number of values returned to Lua
370 * - Argument 1 (string): name of a host
371 * - Return value (host): the corresponding host
373 static int l_host_get_by_name(lua_State * L)
375 const char *name = luaL_checkstring(L, 1);
376 XBT_DEBUG("Getting Host from name...");
377 m_host_t msg_host = MSG_get_host_by_name(name);
379 luaL_error(L, "null Host : MSG_get_host_by_name failed");
381 lua_newtable(L); /* create a table, put the userdata on top of it */
382 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
383 *lua_host = msg_host;
384 luaL_getmetatable(L, HOST_MODULE_NAME);
385 lua_setmetatable(L, -2);
386 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
387 /* remove the args from the stack */
393 * \brief Returns the name of a host.
394 * \param L a Lua state
395 * \return number of values returned to Lua
397 * - Argument 1 (host): a host
398 * - Return value (string): name of this host
400 static int l_host_get_name(lua_State * L)
402 m_host_t ht = sglua_checkhost(L, 1);
403 lua_pushstring(L, MSG_host_get_name(ht));
408 * \brief Returns the number of existing hosts.
409 * \param L a Lua state
410 * \return number of values returned to Lua
412 * - Return value (number): number of hosts
414 static int l_host_number(lua_State * L)
416 lua_pushnumber(L, MSG_get_host_number());
421 * \brief Returns the host given its index.
422 * \param L a Lua state
423 * \return number of values returned to Lua
425 * - Argument 1 (number): an index (1 is the first)
426 * - Return value (host): the host at this index
428 static int l_host_at(lua_State * L)
430 int index = luaL_checkinteger(L, 1);
431 m_host_t host = MSG_get_host_table()[index - 1]; // lua indexing start by 1 (lua[1] <=> C[0])
432 lua_newtable(L); /* create a table, put the userdata on top of it */
433 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
435 luaL_getmetatable(L, HOST_MODULE_NAME);
436 lua_setmetatable(L, -2);
437 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
442 * \brief Returns the host where the current process is located.
443 * \param L a Lua state
444 * \return number of values returned to Lua
446 * - Return value (host): the current host
448 static int l_host_self(lua_State * L)
451 m_host_t host = MSG_host_self();
454 m_host_t* lua_host = (m_host_t*) lua_newuserdata(L, sizeof(m_host_t));
457 luaL_getmetatable(L, HOST_MODULE_NAME);
459 lua_setmetatable(L, -2);
461 lua_setfield(L, -2, "__simgrid_host");
467 * \brief Returns the value of a host property.
468 * \param L a Lua state
469 * \return number of values returned to Lua
471 * - Argument 1 (host): a host
472 * - Argument 2 (string): name of the property to get
473 * - Return value (string): the value of this property
475 static int l_host_get_property_value(lua_State * L)
477 m_host_t ht = sglua_checkhost(L, 1);
478 const char *prop = luaL_checkstring(L, 2);
479 lua_pushstring(L,MSG_host_get_property_value(ht,prop));
484 * \brief Makes the current process sleep for a while.
485 * \param L a Lua state
486 * \return number of values returned to Lua
488 * - Argument 1 (number): duration of the sleep
490 static int l_host_sleep(lua_State *L)
492 int time = luaL_checknumber(L, 1);
493 MSG_process_sleep(time);
498 * \brief Destroys a host.
499 * \param L a Lua state
500 * \return number of values returned to Lua
502 * - Argument 1 (host): the host to destroy
504 static int l_host_destroy(lua_State *L)
506 m_host_t ht = sglua_checkhost(L, 1);
507 __MSG_host_destroy(ht);
511 static const luaL_reg host_functions[] = {
512 {"get_by_name", l_host_get_by_name},
513 {"name", l_host_get_name},
514 {"number", l_host_number},
516 {"self", l_host_self},
517 {"get_prop_value", l_host_get_property_value},
518 {"sleep", l_host_sleep},
519 {"destroy", l_host_destroy},
520 // Bypass XML Methods
521 {"set_function", console_set_function},
522 {"set_property", console_host_set_property},
527 * \brief Returns a string representation of a host.
528 * \param L a Lua state
529 * \return number of values returned to Lua
531 * - Argument 1 (userdata): a host
532 * - Return value (string): a string describing this host
534 static int l_host_tostring(lua_State * L)
536 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
540 static const luaL_reg host_meta[] = {
541 {"__tostring", l_host_tostring},
545 /* ********************************************************************************* */
546 /* lua_stub_generator functions */
547 /* ********************************************************************************* */
549 xbt_dict_t process_function_set;
550 xbt_dynar_t process_list;
551 xbt_dict_t machine_set;
552 static s_process_t process;
554 void s_process_free(void *process)
556 s_process_t *p = (s_process_t *) process;
558 for (i = 0; i < p->argc; i++)
564 static int gras_add_process_function(lua_State * L)
567 const char *process_host = luaL_checkstring(L, 1);
568 const char *process_function = luaL_checkstring(L, 2);
570 if (xbt_dict_is_empty(machine_set)
571 || xbt_dict_is_empty(process_function_set)
572 || xbt_dynar_is_empty(process_list)) {
573 process_function_set = xbt_dict_new();
574 process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
575 machine_set = xbt_dict_new();
578 xbt_dict_set(machine_set, process_host, NULL, NULL);
579 xbt_dict_set(process_function_set, process_function, NULL, NULL);
582 process.argv = xbt_new(char *, 1);
583 process.argv[0] = xbt_strdup(process_function);
584 process.host = strdup(process_host);
587 while (lua_next(L, 3) != 0) {
588 arg = lua_tostring(L, -1);
591 xbt_realloc(process.argv, (process.argc) * sizeof(char *));
592 process.argv[(process.argc) - 1] = xbt_strdup(arg);
594 XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
595 lua_tostring(L, -1));
599 //add to the process list
600 xbt_dynar_push(process_list, &process);
604 static int gras_generate(lua_State * L)
606 const char *project_name = luaL_checkstring(L, 1);
607 generate_sim(project_name);
608 generate_rl(project_name);
609 generate_makefile_local(project_name);
613 /* ********************************************************************************* */
614 /* simgrid.platf API */
615 /* ********************************************************************************* */
617 static const luaL_reg platf_functions[] = {
618 {"open", console_open},
619 {"close", console_close},
620 {"AS_open", console_AS_open},
621 {"AS_close", console_AS_close},
622 {"host_new", console_add_host},
623 {"link_new", console_add_link},
624 {"router_new", console_add_router},
625 {"route_new", console_add_route},
629 /* ********************************************************************************* */
631 /* ********************************************************************************* */
634 * \brief Deploys your application.
635 * \param L a Lua state
636 * \return number of values returned to Lua
638 * - Argument 1 (string): name of the deployment file to load
640 static int launch_application(lua_State * L)
642 const char *file = luaL_checkstring(L, 1);
643 MSG_function_register_default(run_lua_code);
644 MSG_launch_application(file);
649 * \brief Creates the platform.
650 * \param L a Lua state
651 * \return number of values returned to Lua
653 * - Argument 1 (string): name of the platform file to load
655 static int create_environment(lua_State * L)
657 const char *file = luaL_checkstring(L, 1);
658 XBT_DEBUG("Loading environment file %s", file);
659 MSG_create_environment(file);
664 * \brief Prints a log string with debug level.
665 * \param L a Lua state
666 * \return number of values returned to Lua
668 * - Argument 1 (string): the text to print
670 static int debug(lua_State * L)
672 const char *str = luaL_checkstring(L, 1);
673 XBT_DEBUG("%s", str);
678 * \brief Prints a log string with info level.
679 * \param L a Lua state
680 * \return number of values returned to Lua
682 * - Argument 1 (string): the text to print
684 static int info(lua_State * L)
686 const char *str = luaL_checkstring(L, 1);
692 * \brief Runs your application.
693 * \param L a Lua state
694 * \return number of values returned to Lua
696 static int run(lua_State * L)
703 * \brief Cleans the simulation.
704 * \param L a Lua state
705 * \return number of values returned to Lua
707 static int simgrid_gc(lua_State * L)
714 * Register platform for MSG
716 static int msg_register_platform(lua_State * L)
718 /* Tell Simgrid we dont wanna use its parser */
719 //surf_parse = console_parse_platform;
720 surf_parse_reset_callbacks();
721 MSG_create_environment(NULL);
726 * Register platform for Simdag
729 static int sd_register_platform(lua_State * L)
731 //surf_parse = console_parse_platform_wsL07;
732 surf_parse_reset_callbacks();
733 SD_create_environment(NULL);
738 * Register platform for gras
740 static int gras_register_platform(lua_State * L)
742 //surf_parse = console_parse_platform;
743 surf_parse_reset_callbacks();
744 gras_create_environment(NULL);
749 * Register applicaiton for MSG
751 static int msg_register_application(lua_State * L)
753 MSG_function_register_default(run_lua_code);
754 //surf_parse = console_parse_application;
755 MSG_launch_application(NULL);
760 * Register application for gras
762 static int gras_register_application(lua_State * L)
764 gras_function_register_default(run_lua_code);
765 //surf_parse = console_parse_application;
766 gras_launch_application(NULL);
770 static const luaL_Reg simgrid_functions[] = {
771 {"create_environment", create_environment},
772 {"launch_application", launch_application},
777 {"platform", create_environment},
778 {"application", launch_application},
779 /* methods to bypass XML parser */
780 {"msg_register_platform", msg_register_platform},
781 {"sd_register_platform", sd_register_platform},
782 {"msg_register_application", msg_register_application},
783 {"gras_register_platform", gras_register_platform},
784 {"gras_register_application", gras_register_application},
785 /* gras sub generator method */
786 {"gras_set_process_function", gras_add_process_function},
787 {"gras_generate", gras_generate},
791 /* ********************************************************************************* */
792 /* module management functions */
793 /* ********************************************************************************* */
795 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
798 * \brief Opens the simgrid Lua module.
800 * This function is called automatically by the Lua interpreter when some
801 * Lua code requires the "simgrid" module.
803 * \param L the Lua state
805 int luaopen_simgrid(lua_State *L)
807 XBT_DEBUG("luaopen_simgrid *****");
809 /* Get the command line arguments from the lua interpreter */
810 char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
812 argv[0] = (char *) "/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */
814 lua_getglobal(L, "arg");
815 /* if arg is a null value, it means we use lua only as a script to init platform
816 * else it should be a table and then take arg in consideration
818 if (lua_istable(L, -1)) {
822 lua_pushinteger(L, argc - 2);
824 if (lua_isnil(L, -1)) {
827 xbt_assert(lua_isstring(L, -1),
828 "argv[%d] got from lua is no string", argc - 1);
829 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
830 "Too many arguments, please increase LUA_MAX_ARGS_COUNT in %s before recompiling SimGrid if you insist on having more than %d args on command line",
831 __FILE__, LUA_MAX_ARGS_COUNT - 1);
832 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
834 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
839 /* Initialize the MSG core */
840 MSG_global_init(&argc, argv);
841 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
844 /* Keep the context mechanism informed of our lua world today */
845 sglua_maestro_state = L;
847 /* initialize access to my tables by children Lua states */
849 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
851 register_c_functions(L);
857 * \brief Returns whether a Lua state is the maestro state.
858 * \param L a Lua state
859 * \return true if this is maestro
861 int sglua_is_maestro(lua_State* L) {
862 return L == sglua_maestro_state;
866 * \brief Returns the maestro state.
867 * \return the maestro Lua state
869 lua_State* sglua_get_maestro(void) {
870 return sglua_maestro_state;
874 * \brief Registers the task functions into the table simgrid.task.
876 * Also initialize the metatable of the task userdata type.
878 * \param L a lua state
880 static void register_task_functions(lua_State* L)
882 /* create a table simgrid.task and fill it with task functions */
883 luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0);
886 /* create the metatable for tasks, add it to the Lua registry */
887 luaL_newmetatable(L, TASK_MODULE_NAME);
888 /* simgrid.task mt */
889 /* fill the metatable */
890 luaL_openlib(L, NULL, task_meta, 0);
891 /* simgrid.task mt */
892 lua_pushvalue(L, -2);
893 /* simgrid.task mt simgrid.task */
894 /* metatable.__index = simgrid.task
895 * we put the task functions inside the task itself:
896 * this allows to write task:method(args) for
897 * simgrid.task.method(task, args) */
898 lua_setfield(L, -2, "__index");
899 /* simgrid.task mt */
905 * \brief Registers the host functions into the table simgrid.host.
907 * Also initialize the metatable of the host userdata type.
909 * \param L a lua state
911 static void register_host_functions(lua_State* L)
913 /* create a table simgrid.host and fill it with host functions */
914 luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);
917 /* create the metatable for host, add it to the Lua registry */
918 luaL_newmetatable(L, HOST_MODULE_NAME);
919 /* simgrid.host mt */
920 /* fill the metatable */
921 luaL_openlib(L, NULL, host_meta, 0);
922 /* simgrid.host mt */
923 lua_pushvalue(L, -2);
924 /* simgrid.host mt simgrid.host */
925 /* metatable.__index = simgrid.host
926 * we put the host functions inside the host userdata itself:
927 * this allows to write host(args) for
928 * simgrid.host.method(host, args) */
929 // FIXME: cannot work currently, same problem as tasks
930 lua_setfield(L, -2, "__index");
931 /* simgrid.host mt */
937 * \brief Registers the platform functions into the table simgrid.platf.
938 * \param L a lua state
940 static void register_platf_functions(lua_State* L)
942 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
948 * \brief Makes the core functions available to the Lua world.
949 * \param L a Lua world
951 static void register_core_functions(lua_State *L)
953 /* register the core C functions to lua */
954 luaL_register(L, "simgrid", simgrid_functions);
957 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
958 * dummy userdata whose __gc metamethod calls MSG_clean() */
959 lua_newuserdata(L, sizeof(void*));
962 /* simgrid udata mt */
963 lua_pushcfunction(L, simgrid_gc);
964 /* simgrid udata mt simgrid_gc */
965 lua_setfield(L, -2, "__gc");
966 /* simgrid udata mt */
967 lua_setmetatable(L, -2);
969 lua_setfield(L, -2, "__simgrid_loaded");
976 * \brief Creates the simgrid module and make it available to Lua.
977 * \param L a Lua world
979 static void register_c_functions(lua_State *L)
981 register_core_functions(L);
982 register_task_functions(L);
983 register_host_functions(L);
984 register_platf_functions(L);
988 * \brief Runs a Lua function as a new simulated process.
989 * \param argc number of arguments of the function
990 * \param argv name of the Lua function and array of its arguments
991 * \return result of the function
993 static int run_lua_code(int argc, char **argv)
995 XBT_DEBUG("Run lua code %s", argv[0]);
997 /* create a new state, getting globals from maestro */
998 lua_State *L = sglua_clone_maestro();
1001 /* start the function */
1002 lua_getglobal(L, argv[0]);
1003 xbt_assert(lua_isfunction(L, -1),
1004 "There is no Lua function with name `%s'", argv[0]);
1006 /* push arguments onto the stack */
1008 for (i = 1; i < argc; i++)
1009 lua_pushstring(L, argv[i]);
1011 /* call the function */
1012 _XBT_GNUC_UNUSED int err;
1013 err = lua_pcall(L, argc - 1, 1, 0);
1014 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
1015 lua_tostring(L, -1));
1017 /* retrieve result */
1018 if (lua_isnumber(L, -1)) {
1019 res = lua_tonumber(L, -1);
1020 lua_pop(L, 1); /* pop returned value */
1023 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));