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_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 (number): error code
138 static int l_task_execute(lua_State* L)
140 m_task_t task = sglua_checktask(L, 1);
141 int res = MSG_task_execute(task);
142 lua_pushnumber(L, res);
147 * \brief Sends a task to a mailbox and waits for its completion.
148 * \param L a Lua state
149 * \return number of values returned to Lua
151 * - Argument 1 (task): the task to send
152 * - Argument 2 (string): mailbox
154 static int l_task_send(lua_State* L)
156 m_task_t task = sglua_checktask(L, 1);
157 const char* mailbox = luaL_checkstring(L, 2);
161 /* copy my stack into the task, so that the receiver can copy the lua task */
162 MSG_task_set_data(task, L);
163 MSG_error_t res = MSG_task_send(task, mailbox);
164 while (MSG_task_get_data(task) != NULL) {
165 /* don't mess up with my stack: the receiver didn't copy the data yet */
166 MSG_process_sleep(0);
170 /* the receiver is the owner of the task and may destroy it:
171 * remove the C task on my side so that I don't garbage collect it */
172 lua_getfield(L, 1, "__simgrid_task");
174 m_task_t* udata = (m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME);
182 XBT_DEBUG("MSG_task_send failed : Timeout");
184 case MSG_TRANSFER_FAILURE:
185 XBT_DEBUG("MSG_task_send failed : Transfer Failure");
187 case MSG_HOST_FAILURE:
188 XBT_DEBUG("MSG_task_send failed : Host Failure ");
192 ("MSG_task_send failed : Unexpected error , please report this bug");
200 * \brief Receives a task.
201 * \param L a Lua state
202 * \return number of values returned to Lua
204 * - Argument 1 (string): mailbox
205 * - Argument 2 (number, optional): timeout (default is no timeout)
206 * - Return value (task/nil): the task received or nil if the communication
209 static int l_task_recv(lua_State *L)
211 m_task_t task = NULL;
212 const char* mailbox = luaL_checkstring(L, 1);
214 if (lua_gettop(L) >= 2) {
215 /* mailbox timeout */
216 timeout = luaL_checknumber(L, 2);
221 /* no timeout by default */
225 MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
228 /* copy the data directly from sender's stack */
229 lua_State* sender_stack = MSG_task_get_data(task);
230 sglua_copy_value(sender_stack, L);
232 MSG_task_set_data(task, NULL);
237 XBT_DEBUG("MSG_task_receive failed : Timeout");
239 case MSG_TRANSFER_FAILURE:
240 XBT_DEBUG("MSG_task_receive failed : Transfer Failure");
242 case MSG_HOST_FAILURE:
243 XBT_DEBUG("MSG_task_receive failed : Host Failure ");
246 XBT_ERROR("MSG_task_receive failed : Unexpected error , please report this bug");
256 static const luaL_reg task_functions[] = {
258 {"name", l_task_get_name},
259 {"computation_duration", l_task_computation_duration},
260 {"execute", l_task_execute},
261 {"send", l_task_send},
262 {"recv", l_task_recv},
267 * \brief Finalizes the userdata of a task.
268 * \param L a Lua state
269 * \return number of values returned to Lua
271 * - Argument 1 (userdata): a C task, possibly NULL if it was sent to another
274 static int l_task_gc(lua_State* L)
277 m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
278 /* the task is NULL if I sent it to someone else */
280 MSG_task_destroy(task);
286 * \brief Returns a string representation of a C task.
287 * \param L a Lua state
288 * \return number of values returned to Lua
290 * - Argument 1 (userdata): a task
291 * - Return value (string): a string describing this task
293 static int l_task_tostring(lua_State* L)
295 m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
296 lua_pushfstring(L, "Task: %p", task);
301 * \brief Metamethods of both a task table and the userdata inside it.
303 static const luaL_reg task_meta[] = {
304 {"__gc", l_task_gc}, /* will be called only for userdata */
305 {"__tostring", l_task_tostring},
309 /* ********************************************************************************* */
310 /* simgrid.host API */
311 /* ********************************************************************************* */
314 * \brief Ensures that a value in the stack is a host and returns it.
315 * \param L a Lua state
316 * \param index an index in the Lua stack
317 * \return the C host corresponding to this Lua host
319 static m_host_t sglua_checkhost(lua_State * L, int index)
322 luaL_checktype(L, index, LUA_TTABLE);
323 lua_getfield(L, index, "__simgrid_host");
324 pi = (m_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
326 luaL_typerror(L, index, HOST_MODULE_NAME);
329 luaL_error(L, "null Host");
335 * \brief Returns a host given its name.
336 * \param L a Lua state
337 * \return number of values returned to Lua
339 * - Argument 1 (string): name of a host
340 * - Return value (host): the corresponding host
342 static int l_host_get_by_name(lua_State * L)
344 const char *name = luaL_checkstring(L, 1);
345 XBT_DEBUG("Getting Host from name...");
346 m_host_t msg_host = MSG_get_host_by_name(name);
348 luaL_error(L, "null Host : MSG_get_host_by_name failed");
350 lua_newtable(L); /* create a table, put the userdata on top of it */
351 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
352 *lua_host = msg_host;
353 luaL_getmetatable(L, HOST_MODULE_NAME);
354 lua_setmetatable(L, -2);
355 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
356 /* remove the args from the stack */
362 * \brief Returns the name of a host.
363 * \param L a Lua state
364 * \return number of values returned to Lua
366 * - Argument 1 (host): a host
367 * - Return value (string): name of this host
369 static int l_host_get_name(lua_State * L)
371 m_host_t ht = sglua_checkhost(L, 1);
372 lua_pushstring(L, MSG_host_get_name(ht));
377 * \brief Returns the number of existing hosts.
378 * \param L a Lua state
379 * \return number of values returned to Lua
381 * - Return value (number): number of hosts
383 static int l_host_number(lua_State * L)
385 lua_pushnumber(L, MSG_get_host_number());
390 * \brief Returns the host given its index.
391 * \param L a Lua state
392 * \return number of values returned to Lua
394 * - Argument 1 (number): an index (1 is the first)
395 * - Return value (host): the host at this index
397 static int l_host_at(lua_State * L)
399 int index = luaL_checkinteger(L, 1);
400 m_host_t host = MSG_get_host_table()[index - 1]; // lua indexing start by 1 (lua[1] <=> C[0])
401 lua_newtable(L); /* create a table, put the userdata on top of it */
402 m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
404 luaL_getmetatable(L, HOST_MODULE_NAME);
405 lua_setmetatable(L, -2);
406 lua_setfield(L, -2, "__simgrid_host"); /* put the userdata as field of the table */
411 * \brief Returns the host where the current process is located.
412 * \param L a Lua state
413 * \return number of values returned to Lua
415 * - Return value (host): the current host
417 static int l_host_self(lua_State * L)
420 m_host_t host = MSG_host_self();
423 m_host_t* lua_host = (m_host_t*) lua_newuserdata(L, sizeof(m_host_t));
426 luaL_getmetatable(L, HOST_MODULE_NAME);
428 lua_setmetatable(L, -2);
430 lua_setfield(L, -2, "__simgrid_host");
436 * \brief Returns the value of a host property.
437 * \param L a Lua state
438 * \return number of values returned to Lua
440 * - Argument 1 (host): a host
441 * - Argument 2 (string): name of the property to get
442 * - Return value (string): the value of this property
444 static int l_host_get_property_value(lua_State * L)
446 m_host_t ht = sglua_checkhost(L, 1);
447 const char *prop = luaL_checkstring(L, 2);
448 lua_pushstring(L,MSG_host_get_property_value(ht,prop));
453 * \brief Makes the current process sleep for a while.
454 * \param L a Lua state
455 * \return number of values returned to Lua
457 * - Argument 1 (number): duration of the sleep
459 static int l_host_sleep(lua_State *L)
461 int time = luaL_checknumber(L, 1);
462 MSG_process_sleep(time);
467 * \brief Destroys a host.
468 * \param L a Lua state
469 * \return number of values returned to Lua
471 * - Argument 1 (host): the host to destroy
473 static int l_host_destroy(lua_State *L)
475 m_host_t ht = sglua_checkhost(L, 1);
476 __MSG_host_destroy(ht);
480 static const luaL_reg host_functions[] = {
481 {"get_by_name", l_host_get_by_name},
482 {"name", l_host_get_name},
483 {"number", l_host_number},
485 {"self", l_host_self},
486 {"get_prop_value", l_host_get_property_value},
487 {"sleep", l_host_sleep},
488 {"destroy", l_host_destroy},
489 // Bypass XML Methods
490 {"set_function", console_set_function},
491 {"set_property", console_host_set_property},
496 * \brief Returns a string representation of a host.
497 * \param L a Lua state
498 * \return number of values returned to Lua
500 * - Argument 1 (userdata): a host
501 * - Return value (string): a string describing this host
503 static int l_host_tostring(lua_State * L)
505 lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
509 static const luaL_reg host_meta[] = {
510 {"__tostring", l_host_tostring},
514 /* ********************************************************************************* */
515 /* lua_stub_generator functions */
516 /* ********************************************************************************* */
518 xbt_dict_t process_function_set;
519 xbt_dynar_t process_list;
520 xbt_dict_t machine_set;
521 static s_process_t process;
523 void s_process_free(void *process)
525 s_process_t *p = (s_process_t *) process;
527 for (i = 0; i < p->argc; i++)
533 static int gras_add_process_function(lua_State * L)
536 const char *process_host = luaL_checkstring(L, 1);
537 const char *process_function = luaL_checkstring(L, 2);
539 if (xbt_dict_is_empty(machine_set)
540 || xbt_dict_is_empty(process_function_set)
541 || xbt_dynar_is_empty(process_list)) {
542 process_function_set = xbt_dict_new();
543 process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
544 machine_set = xbt_dict_new();
547 xbt_dict_set(machine_set, process_host, NULL, NULL);
548 xbt_dict_set(process_function_set, process_function, NULL, NULL);
551 process.argv = xbt_new(char *, 1);
552 process.argv[0] = xbt_strdup(process_function);
553 process.host = strdup(process_host);
556 while (lua_next(L, 3) != 0) {
557 arg = lua_tostring(L, -1);
560 xbt_realloc(process.argv, (process.argc) * sizeof(char *));
561 process.argv[(process.argc) - 1] = xbt_strdup(arg);
563 XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
564 lua_tostring(L, -1));
568 //add to the process list
569 xbt_dynar_push(process_list, &process);
573 static int gras_generate(lua_State * L)
575 const char *project_name = luaL_checkstring(L, 1);
576 generate_sim(project_name);
577 generate_rl(project_name);
578 generate_makefile_local(project_name);
582 /* ********************************************************************************* */
583 /* simgrid.platf API */
584 /* ********************************************************************************* */
586 static const luaL_reg platf_functions[] = {
587 {"open", console_open},
588 {"close", console_close},
589 {"AS_open", console_AS_open},
590 {"AS_close", console_AS_close},
591 {"host_new", console_add_host},
592 {"link_new", console_add_link},
593 {"router_new", console_add_router},
594 {"route_new", console_add_route},
598 /* ********************************************************************************* */
600 /* ********************************************************************************* */
603 * \brief Deploys your application.
604 * \param L a Lua state
605 * \return number of values returned to Lua
607 * - Argument 1 (string): name of the deployment file to load
609 static int launch_application(lua_State * L)
611 const char *file = luaL_checkstring(L, 1);
612 MSG_function_register_default(run_lua_code);
613 MSG_launch_application(file);
618 * \brief Creates the platform.
619 * \param L a Lua state
620 * \return number of values returned to Lua
622 * - Argument 1 (string): name of the platform file to load
624 static int create_environment(lua_State * L)
626 const char *file = luaL_checkstring(L, 1);
627 XBT_DEBUG("Loading environment file %s", file);
628 MSG_create_environment(file);
633 * \brief Prints a log string with debug level.
634 * \param L a Lua state
635 * \return number of values returned to Lua
637 * - Argument 1 (string): the text to print
639 static int debug(lua_State * L)
641 const char *str = luaL_checkstring(L, 1);
642 XBT_DEBUG("%s", str);
647 * \brief Prints a log string with info level.
648 * \param L a Lua state
649 * \return number of values returned to Lua
651 * - Argument 1 (string): the text to print
653 static int info(lua_State * L)
655 const char *str = luaL_checkstring(L, 1);
661 * \brief Runs your application.
662 * \param L a Lua state
663 * \return number of values returned to Lua
665 static int run(lua_State * L)
672 * \brief Cleans the simulation.
673 * \param L a Lua state
674 * \return number of values returned to Lua
676 static int simgrid_gc(lua_State * L)
683 * Register platform for MSG
685 static int msg_register_platform(lua_State * L)
687 /* Tell Simgrid we dont wanna use its parser */
688 //surf_parse = console_parse_platform;
689 surf_parse_reset_callbacks();
690 MSG_create_environment(NULL);
695 * Register platform for Simdag
698 static int sd_register_platform(lua_State * L)
700 //surf_parse = console_parse_platform_wsL07;
701 surf_parse_reset_callbacks();
702 SD_create_environment(NULL);
707 * Register platform for gras
709 static int gras_register_platform(lua_State * L)
711 //surf_parse = console_parse_platform;
712 surf_parse_reset_callbacks();
713 gras_create_environment(NULL);
718 * Register applicaiton for MSG
720 static int msg_register_application(lua_State * L)
722 MSG_function_register_default(run_lua_code);
723 //surf_parse = console_parse_application;
724 MSG_launch_application(NULL);
729 * Register application for gras
731 static int gras_register_application(lua_State * L)
733 gras_function_register_default(run_lua_code);
734 //surf_parse = console_parse_application;
735 gras_launch_application(NULL);
739 static const luaL_Reg simgrid_functions[] = {
740 {"create_environment", create_environment},
741 {"launch_application", launch_application},
746 {"platform", create_environment},
747 {"application", launch_application},
748 /* methods to bypass XML parser */
749 {"msg_register_platform", msg_register_platform},
750 {"sd_register_platform", sd_register_platform},
751 {"msg_register_application", msg_register_application},
752 {"gras_register_platform", gras_register_platform},
753 {"gras_register_application", gras_register_application},
754 /* gras sub generator method */
755 {"gras_set_process_function", gras_add_process_function},
756 {"gras_generate", gras_generate},
760 /* ********************************************************************************* */
761 /* module management functions */
762 /* ********************************************************************************* */
764 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
767 * \brief Opens the simgrid Lua module.
769 * This function is called automatically by the Lua interpreter when some
770 * Lua code requires the "simgrid" module.
772 * \param L the Lua state
774 int luaopen_simgrid(lua_State *L)
776 XBT_DEBUG("luaopen_simgrid *****");
778 /* Get the command line arguments from the lua interpreter */
779 char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
781 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? */
783 lua_getglobal(L, "arg");
784 /* if arg is a null value, it means we use lua only as a script to init platform
785 * else it should be a table and then take arg in consideration
787 if (lua_istable(L, -1)) {
791 lua_pushinteger(L, argc - 2);
793 if (lua_isnil(L, -1)) {
796 xbt_assert(lua_isstring(L, -1),
797 "argv[%d] got from lua is no string", argc - 1);
798 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
799 "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",
800 __FILE__, LUA_MAX_ARGS_COUNT - 1);
801 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
803 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
808 /* Initialize the MSG core */
809 MSG_global_init(&argc, argv);
810 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
813 /* Keep the context mechanism informed of our lua world today */
814 sglua_maestro_state = L;
816 /* initialize access to my tables by children Lua states */
818 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
820 register_c_functions(L);
826 * \brief Returns whether a Lua state is the maestro state.
827 * \param L a Lua state
828 * \return true if this is maestro
830 int sglua_is_maestro(lua_State* L) {
831 return L == sglua_maestro_state;
835 * \brief Returns the maestro state.
836 * \return the maestro Lua state
838 lua_State* sglua_get_maestro(void) {
839 return sglua_maestro_state;
843 * \brief Registers the task functions into the table simgrid.task.
845 * Also initialize the metatable of the task userdata type.
847 * \param L a lua state
849 static void register_task_functions(lua_State* L)
851 /* create a table simgrid.task and fill it with task functions */
852 luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0);
855 /* create the metatable for tasks, add it to the Lua registry */
856 luaL_newmetatable(L, TASK_MODULE_NAME);
857 /* simgrid.task mt */
858 /* fill the metatable */
859 luaL_openlib(L, NULL, task_meta, 0);
860 /* simgrid.task mt */
861 lua_pushvalue(L, -2);
862 /* simgrid.task mt simgrid.task */
863 /* metatable.__index = simgrid.task
864 * we put the task functions inside the task itself:
865 * this allows to write task:method(args) for
866 * simgrid.task.method(task, args) */
867 lua_setfield(L, -2, "__index");
868 /* simgrid.task mt */
874 * \brief Registers the host functions into the table simgrid.host.
876 * Also initialize the metatable of the host userdata type.
878 * \param L a lua state
880 static void register_host_functions(lua_State* L)
882 /* create a table simgrid.host and fill it with host functions */
883 luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);
886 /* create the metatable for host, add it to the Lua registry */
887 luaL_newmetatable(L, HOST_MODULE_NAME);
888 /* simgrid.host mt */
889 /* fill the metatable */
890 luaL_openlib(L, NULL, host_meta, 0);
891 /* simgrid.host mt */
892 lua_pushvalue(L, -2);
893 /* simgrid.host mt simgrid.host */
894 /* metatable.__index = simgrid.host
895 * we put the host functions inside the host userdata itself:
896 * this allows to write host(args) for
897 * simgrid.host.method(host, args) */
898 // FIXME: cannot work currently, same problem as tasks
899 lua_setfield(L, -2, "__index");
900 /* simgrid.host mt */
906 * \brief Registers the platform functions into the table simgrid.platf.
907 * \param L a lua state
909 static void register_platf_functions(lua_State* L)
911 luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);
917 * \brief Makes the core functions available to the Lua world.
918 * \param L a Lua world
920 static void register_core_functions(lua_State *L)
922 /* register the core C functions to lua */
923 luaL_register(L, "simgrid", simgrid_functions);
926 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
927 * dummy userdata whose __gc metamethod calls MSG_clean() */
928 lua_newuserdata(L, sizeof(void*));
931 /* simgrid udata mt */
932 lua_pushcfunction(L, simgrid_gc);
933 /* simgrid udata mt simgrid_gc */
934 lua_setfield(L, -2, "__gc");
935 /* simgrid udata mt */
936 lua_setmetatable(L, -2);
938 lua_setfield(L, -2, "__simgrid_loaded");
945 * \brief Creates the simgrid module and make it available to Lua.
946 * \param L a Lua world
948 static void register_c_functions(lua_State *L)
950 register_core_functions(L);
951 register_task_functions(L);
952 register_host_functions(L);
953 register_platf_functions(L);
957 * \brief Runs a Lua function as a new simulated process.
958 * \param argc number of arguments of the function
959 * \param argv name of the Lua function and array of its arguments
960 * \return result of the function
962 static int run_lua_code(int argc, char **argv)
964 XBT_DEBUG("Run lua code %s", argv[0]);
966 /* create a new state, getting globals from maestro */
967 lua_State *L = sglua_clone_maestro();
970 /* start the function */
971 lua_getglobal(L, argv[0]);
972 xbt_assert(lua_isfunction(L, -1),
973 "There is no Lua function with name `%s'", argv[0]);
975 /* push arguments onto the stack */
977 for (i = 1; i < argc; i++)
978 lua_pushstring(L, argv[i]);
980 /* call the function */
981 _XBT_GNUC_UNUSED int err;
982 err = lua_pcall(L, argc - 1, 1, 0);
983 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
984 lua_tostring(L, -1));
986 /* retrieve result */
987 if (lua_isnumber(L, -1)) {
988 res = lua_tonumber(L, -1);
989 lua_pop(L, 1); /* pop returned value */
992 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));