Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
71eb3070d30bba2f2079dae11570ed545ba002cc
[simgrid.git] / src / bindings / lua / simgrid_lua.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "simgrid_lua.h"
10 #include "lua_state_cloner.h"
11 #include "lua_utils.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
14
15 #define TASK_MODULE_NAME "simgrid.Task"
16 #define HOST_MODULE_NAME "simgrid.Host"
17 // Surf (bypass XML)
18 #define LINK_MODULE_NAME "simgrid.Link"
19 #define ROUTE_MODULE_NAME "simgrid.Route"
20 #define AS_MODULE_NAME "simgrid.AS"
21 #define TRACE_MODULE_NAME "simgrid.Trace"
22
23 static lua_State* sglua_maestro_state;
24
25 static void register_c_functions(lua_State *L);
26
27 /**
28  * \brief Like luaL_checkudata, with additional debug logs.
29  * \param L a lua state
30  * \param ud index of the userdata to check in the stack
31  * \param tname key of the metatable of this userdata in the registry
32  */
33 static void* my_checkudata(lua_State* L, int ud, const char* tname) {
34
35   XBT_DEBUG("Checking the userdata: ud = %d", ud);
36   sglua_stack_dump("my_checkudata: ", L);
37   void* p = lua_touserdata(L, ud);
38   lua_getfield(L, LUA_REGISTRYINDEX, tname);
39   const void* correct_mt = lua_topointer(L, -1);
40
41   int has_mt = lua_getmetatable(L, ud);
42   XBT_DEBUG("Checking the userdata: has metatable ? %d", has_mt);
43   const void* actual_mt = NULL;
44   if (has_mt) {
45     actual_mt = lua_topointer(L, -1);
46     lua_pop(L, 1);
47   }
48   XBT_DEBUG("Checking the task's metatable: expected %p, found %p", correct_mt, actual_mt);
49   sglua_stack_dump("my_checkudata: ", L);
50
51   if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
52     luaL_typerror(L, ud, tname);
53   lua_pop(L, 2);
54   return p;
55 }
56
57 /**
58  * @brief Ensures that a value is a valid task and returns it.
59  * @param L a Lua state
60  * @param index an index in the Lua stack
61  * @return the task at this index
62  */
63 static m_task_t checkTask(lua_State* L, int index)
64 {
65   sglua_stack_dump("checkTask: ", L);
66   luaL_checktype(L, index, LUA_TTABLE);
67                                   /* ... task ... */
68   lua_getfield(L, index, "__simgrid_task");
69                                   /* ... task ... ctask */
70   m_task_t task = *((m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
71   lua_pop(L, 1);
72                                   /* ... task ... */
73
74   if (task == NULL) {
75     luaL_error(L, "This task was sent to someone else, you cannot access it anymore");
76   }
77
78   return task;
79 }
80
81 /* ********************************************************************************* */
82 /*                           wrapper functions                                       */
83 /* ********************************************************************************* */
84
85 /**
86  * \brief Creates a new task and leaves it onto the stack.
87  *
88  * Lua arguments:
89  * - task name (string)
90  * - computation size (integer)
91  * - communication size (integer)
92  */
93 static int Task_new(lua_State* L)
94 {
95   XBT_DEBUG("Task new");
96   const char* name = luaL_checkstring(L, 1);
97   int comp_size = luaL_checkint(L, 2);
98   int msg_size = luaL_checkint(L, 3);
99                                   /* name comp comm */
100   lua_settop(L, 0);
101                                   /* -- */
102   m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
103
104   lua_newtable(L);
105                                   /* task */
106   m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t));
107                                   /* task ctask */
108   *lua_task = msg_task;
109   luaL_getmetatable(L, TASK_MODULE_NAME);
110                                   /* task ctask mt */
111   lua_setmetatable(L, -2);
112                                   /* task ctask */
113   lua_setfield(L, -2, "__simgrid_task");
114                                   /* task */
115   return 1;
116 }
117
118 static int Task_get_name(lua_State* L)
119 {
120   m_task_t task = checkTask(L, 1);
121   lua_pushstring(L, MSG_task_get_name(task));
122   return 1;
123 }
124
125 static int Task_computation_duration(lua_State* L)
126 {
127   m_task_t task = checkTask(L, 1);
128   lua_pushnumber(L, MSG_task_get_compute_duration(task));
129   return 1;
130 }
131
132 static int Task_execute(lua_State* L)
133 {
134   m_task_t task = checkTask(L, 1);
135   int res = MSG_task_execute(task);
136   lua_pushnumber(L, res);
137   return 1;
138 }
139
140 static int Task_send(lua_State* L)
141 {
142   m_task_t task = checkTask(L, 1);
143   const char* mailbox = luaL_checkstring(L, 2);
144                                   /* task mailbox */
145   lua_settop(L, 1);
146                                   /* task */
147   /* copy my stack into the task, so that the receiver can copy the lua task */
148   MSG_task_set_data(task, L);
149   MSG_error_t res = MSG_task_send(task, mailbox);
150   while (MSG_task_get_data(task) != NULL) {
151     /* don't mess up with my stack: the receiver didn't copy the data yet */
152     MSG_process_sleep(0);
153   }
154
155   if (res == MSG_OK) {
156     /* the receiver is the owner of the task and may destroy it:
157      * remove the C task on my side so that I don't garbage collect it */
158     lua_getfield(L, 1, "__simgrid_task");
159                                   /* task ctask */
160     m_task_t* udata = (m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME);
161     *udata = NULL;
162     lua_pop(L, 1);
163                                   /* task */
164   }
165   else {
166     switch (res) {
167     case MSG_TIMEOUT:
168       XBT_DEBUG("MSG_task_send failed : Timeout");
169       break;
170     case MSG_TRANSFER_FAILURE:
171       XBT_DEBUG("MSG_task_send failed : Transfer Failure");
172       break;
173     case MSG_HOST_FAILURE:
174       XBT_DEBUG("MSG_task_send failed : Host Failure ");
175       break;
176     default:
177       XBT_ERROR
178           ("MSG_task_send failed : Unexpected error , please report this bug");
179       break;
180     }
181   }
182   return 0;
183 }
184
185 static int Task_recv_with_timeout(lua_State *L)
186 {
187   m_task_t task = NULL;
188   const char* mailbox = luaL_checkstring(L, 1);
189   int timeout = luaL_checknumber(L, 2);
190                                   /* mailbox timeout */
191   lua_settop(L, 0);
192                                   /* -- */
193   MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
194
195   if (res == MSG_OK) {
196     /* copy the data directly from sender's stack */
197     lua_State* sender_stack = MSG_task_get_data(task);
198     sglua_copy_value(sender_stack, L);
199                                   /* task */
200     MSG_task_set_data(task, NULL);
201   }
202   else {
203     switch (res) {
204     case MSG_TIMEOUT:
205       XBT_DEBUG("MSG_task_receive failed : Timeout");
206       break;
207     case MSG_TRANSFER_FAILURE:
208       XBT_DEBUG("MSG_task_receive failed : Transfer Failure");
209       break;
210     case MSG_HOST_FAILURE:
211       XBT_DEBUG("MSG_task_receive failed : Host Failure ");
212       break;
213     default:
214       XBT_ERROR("MSG_task_receive failed : Unexpected error , please report this bug");
215       break;
216     }
217     lua_pushnil(L);
218                                   /* nil */
219   }
220                                   /* task/nil */
221   return 1;
222 }
223
224 static int Task_recv(lua_State * L)
225 {
226   lua_pushnumber(L, -1.0);
227   return Task_recv_with_timeout(L);
228 }
229
230 static const luaL_reg Task_methods[] = {
231   {"new", Task_new},
232   {"name", Task_get_name},
233   {"computation_duration", Task_computation_duration},
234   {"execute", Task_execute},
235   {"send", Task_send},
236   {"recv", Task_recv},
237   {"recv_timeout", Task_recv_with_timeout},
238   {NULL, NULL}
239 };
240
241 static int Task_gc(lua_State* L)
242 {
243                                   /* ctask */
244   m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
245   /* the task is NULL if I sent it to someone else */
246   if (task != NULL) {
247     MSG_task_destroy(task);
248   }
249   return 0;
250 }
251
252 static int Task_tostring(lua_State* L)
253 {
254   m_task_t task = checkTask(L, 1);
255   lua_pushfstring(L, "Task: %p", task);
256   return 1;
257 }
258
259 static const luaL_reg Task_meta[] = {
260   {"__gc", Task_gc},
261   {"__tostring", Task_tostring},
262   {NULL, NULL}
263 };
264
265 /**
266  * Host
267  */
268 static m_host_t checkHost(lua_State * L, int index)
269 {
270   m_host_t *pi, ht;
271   luaL_checktype(L, index, LUA_TTABLE);
272   lua_getfield(L, index, "__simgrid_host");
273   pi = (m_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
274   if (pi == NULL)
275     luaL_typerror(L, index, HOST_MODULE_NAME);
276   ht = *pi;
277   if (!ht)
278     luaL_error(L, "null Host");
279   lua_pop(L, 1);
280   return ht;
281 }
282
283 static int Host_get_by_name(lua_State * L)
284 {
285   const char *name = luaL_checkstring(L, 1);
286   XBT_DEBUG("Getting Host from name...");
287   m_host_t msg_host = MSG_get_host_by_name(name);
288   if (!msg_host) {
289     luaL_error(L, "null Host : MSG_get_host_by_name failed");
290   }
291   lua_newtable(L);              /* create a table, put the userdata on top of it */
292   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
293   *lua_host = msg_host;
294   luaL_getmetatable(L, HOST_MODULE_NAME);
295   lua_setmetatable(L, -2);
296   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
297   /* remove the args from the stack */
298   lua_remove(L, 1);
299   return 1;
300 }
301
302 static int Host_get_name(lua_State * L)
303 {
304   m_host_t ht = checkHost(L, -1);
305   lua_pushstring(L, MSG_host_get_name(ht));
306   return 1;
307 }
308
309 static int Host_number(lua_State * L)
310 {
311   lua_pushnumber(L, MSG_get_host_number());
312   return 1;
313 }
314
315 static int Host_at(lua_State * L)
316 {
317   int index = luaL_checkinteger(L, 1);
318   m_host_t host = MSG_get_host_table()[index - 1];      // lua indexing start by 1 (lua[1] <=> C[0])
319   lua_newtable(L);              /* create a table, put the userdata on top of it */
320   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
321   *lua_host = host;
322   luaL_getmetatable(L, HOST_MODULE_NAME);
323   lua_setmetatable(L, -2);
324   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
325   return 1;
326
327 }
328
329 static int Host_self(lua_State * L)
330 {
331                                   /* -- */
332   m_host_t host = MSG_host_self();
333   lua_newtable(L);
334                                   /* table */
335   m_host_t* lua_host = (m_host_t*) lua_newuserdata(L, sizeof(m_host_t));
336                                   /* table ud */
337   *lua_host = host;
338   luaL_getmetatable(L, HOST_MODULE_NAME);
339                                   /* table ud mt */
340   lua_setmetatable(L, -2);
341                                   /* table ud */
342   lua_setfield(L, -2, "__simgrid_host");
343                                   /* table */
344   return 1;
345 }
346
347 static int Host_get_property_value(lua_State * L)
348 {
349   m_host_t ht = checkHost(L, -2);
350   const char *prop = luaL_checkstring(L, -1);
351   lua_pushstring(L,MSG_host_get_property_value(ht,prop));
352   return 1;
353 }
354
355 static int Host_sleep(lua_State *L)
356 {
357   int time = luaL_checknumber(L, -1);
358   MSG_process_sleep(time);
359   return 1;
360 }
361
362 static int Host_destroy(lua_State *L)
363 {
364   m_host_t ht = checkHost(L, -1);
365   __MSG_host_destroy(ht);
366   return 1;
367 }
368
369 /* ********************************************************************************* */
370 /*                           lua_stub_generator functions                            */
371 /* ********************************************************************************* */
372
373 xbt_dict_t process_function_set;
374 xbt_dynar_t process_list;
375 xbt_dict_t machine_set;
376 static s_process_t process;
377
378 void s_process_free(void *process)
379 {
380   s_process_t *p = (s_process_t *) process;
381   int i;
382   for (i = 0; i < p->argc; i++)
383     free(p->argv[i]);
384   free(p->argv);
385   free(p->host);
386 }
387
388 static int gras_add_process_function(lua_State * L)
389 {
390   const char *arg;
391   const char *process_host = luaL_checkstring(L, 1);
392   const char *process_function = luaL_checkstring(L, 2);
393
394   if (xbt_dict_is_empty(machine_set)
395       || xbt_dict_is_empty(process_function_set)
396       || xbt_dynar_is_empty(process_list)) {
397     process_function_set = xbt_dict_new();
398     process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
399     machine_set = xbt_dict_new();
400   }
401
402   xbt_dict_set(machine_set, process_host, NULL, NULL);
403   xbt_dict_set(process_function_set, process_function, NULL, NULL);
404
405   process.argc = 1;
406   process.argv = xbt_new(char *, 1);
407   process.argv[0] = xbt_strdup(process_function);
408   process.host = strdup(process_host);
409
410   lua_pushnil(L);
411   while (lua_next(L, 3) != 0) {
412     arg = lua_tostring(L, -1);
413     process.argc++;
414     process.argv =
415         xbt_realloc(process.argv, (process.argc) * sizeof(char *));
416     process.argv[(process.argc) - 1] = xbt_strdup(arg);
417
418     XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
419            lua_tostring(L, -1));
420     lua_pop(L, 1);
421   }
422   lua_pop(L, 1);
423   //add to the process list
424   xbt_dynar_push(process_list, &process);
425   return 0;
426 }
427
428
429 static int gras_generate(lua_State * L)
430 {
431   const char *project_name = luaL_checkstring(L, 1);
432   generate_sim(project_name);
433   generate_rl(project_name);
434   generate_makefile_local(project_name);
435   return 0;
436 }
437
438 /***********************************
439  *      Tracing
440  **********************************/
441 static int trace_start(lua_State *L)
442 {
443 #ifdef HAVE_TRACING
444   TRACE_start();
445 #endif
446   return 1;
447 }
448
449 static int trace_category(lua_State * L)
450 {
451 #ifdef HAVE_TRACING
452   TRACE_category(luaL_checkstring(L, 1));
453 #endif
454   return 1;
455 }
456
457 static int trace_set_task_category(lua_State *L)
458 {
459 #ifdef HAVE_TRACING
460   TRACE_msg_set_task_category(checkTask(L, -2), luaL_checkstring(L, -1));
461 #endif
462   return 1;
463 }
464
465 static int trace_end(lua_State *L)
466 {
467 #ifdef HAVE_TRACING
468   TRACE_end();
469 #endif
470   return 1;
471 }
472
473 // *********** Register Methods ******************************************* //
474
475 /*
476  * Host Methods
477  */
478 static const luaL_reg Host_methods[] = {
479   {"getByName", Host_get_by_name},
480   {"name", Host_get_name},
481   {"number", Host_number},
482   {"at", Host_at},
483   {"self", Host_self},
484   {"getPropValue", Host_get_property_value},
485   {"sleep", Host_sleep},
486   {"destroy", Host_destroy},
487   // Bypass XML Methods
488   {"setFunction", console_set_function},
489   {"setProperty", console_host_set_property},
490   {NULL, NULL}
491 };
492
493 static int Host_gc(lua_State * L)
494 {
495   m_host_t ht = checkHost(L, -1);
496   if (ht)
497     ht = NULL;
498   return 0;
499 }
500
501 static int Host_tostring(lua_State * L)
502 {
503   lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
504   return 1;
505 }
506
507 static const luaL_reg Host_meta[] = {
508   {"__gc", Host_gc},
509   {"__tostring", Host_tostring},
510   {0, 0}
511 };
512
513 /*
514  * AS Methods
515  */
516 static const luaL_reg AS_methods[] = {
517   {"new", console_add_AS},
518   {"addHost", console_add_host},
519   {"addLink", console_add_link},
520   {"addRoute", console_add_route},
521   {NULL, NULL}
522 };
523
524 /**
525  * Tracing Functions
526  */
527 static const luaL_reg Trace_methods[] = {
528   {"start", trace_start},
529   {"category", trace_category},
530   {"setTaskCategory", trace_set_task_category},
531   {"finish", trace_end},
532   {NULL, NULL}
533 };
534
535 /*
536  * Environment related
537  */
538
539 /**
540  * @brief Runs a Lua function as a new simulated process.
541  * @param argc number of arguments of the function
542  * @param argv name of the Lua function and array of its arguments
543  * @return result of the function
544  */
545 static int run_lua_code(int argc, char **argv)
546 {
547   XBT_DEBUG("Run lua code %s", argv[0]);
548
549   lua_State *L = sglua_clone_maestro();
550   int res = 1;
551
552   /* start the function */
553   lua_getglobal(L, argv[0]);
554   xbt_assert(lua_isfunction(L, -1),
555               "The lua function %s does not seem to exist", argv[0]);
556
557   /* push arguments onto the stack */
558   int i;
559   for (i = 1; i < argc; i++)
560     lua_pushstring(L, argv[i]);
561
562   /* call the function */
563   _XBT_GNUC_UNUSED int err;
564   err = lua_pcall(L, argc - 1, 1, 0);
565   xbt_assert(err == 0, "error running function `%s': %s", argv[0],
566               lua_tostring(L, -1));
567
568   /* retrieve result */
569   if (lua_isnumber(L, -1)) {
570     res = lua_tonumber(L, -1);
571     lua_pop(L, 1);              /* pop returned value */
572   }
573
574   XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
575
576   return res;
577 }
578
579 static int launch_application(lua_State * L)
580 {
581   const char *file = luaL_checkstring(L, 1);
582   MSG_function_register_default(run_lua_code);
583   MSG_launch_application(file);
584   return 0;
585 }
586
587 static int create_environment(lua_State * L)
588 {
589   const char *file = luaL_checkstring(L, 1);
590   XBT_DEBUG("Loading environment file %s", file);
591   MSG_create_environment(file);
592   return 0;
593 }
594
595 static int debug(lua_State * L)
596 {
597   const char *str = luaL_checkstring(L, 1);
598   XBT_DEBUG("%s", str);
599   return 0;
600 }
601
602 static int info(lua_State * L)
603 {
604   const char *str = luaL_checkstring(L, 1);
605   XBT_INFO("%s", str);
606   return 0;
607 }
608
609 static int run(lua_State * L)
610 {
611   MSG_main();
612   return 0;
613 }
614
615 static int clean(lua_State * L)
616 {
617   MSG_clean();
618   return 0;
619 }
620
621 /*
622  * Bypass XML Parser (lua console)
623  */
624
625 /*
626  * Register platform for MSG
627  */
628 static int msg_register_platform(lua_State * L)
629 {
630   /* Tell Simgrid we dont wanna use its parser */
631   surf_parse = console_parse_platform;
632   surf_parse_reset_callbacks();
633   MSG_create_environment(NULL);
634   return 0;
635 }
636
637 /*
638  * Register platform for Simdag
639  */
640
641 static int sd_register_platform(lua_State * L)
642 {
643   surf_parse = console_parse_platform_wsL07;
644   surf_parse_reset_callbacks();
645   SD_create_environment(NULL);
646   return 0;
647 }
648
649 /*
650  * Register platform for gras
651  */
652 static int gras_register_platform(lua_State * L)
653 {
654   /* Tell Simgrid we dont wanna use surf parser */
655   surf_parse = console_parse_platform;
656   surf_parse_reset_callbacks();
657   gras_create_environment(NULL);
658   return 0;
659 }
660
661 /**
662  * Register applicaiton for MSG
663  */
664 static int msg_register_application(lua_State * L)
665 {
666   MSG_function_register_default(run_lua_code);
667   surf_parse = console_parse_application;
668   MSG_launch_application(NULL);
669   return 0;
670 }
671
672 /*
673  * Register application for gras
674  */
675 static int gras_register_application(lua_State * L)
676 {
677   gras_function_register_default(run_lua_code);
678   surf_parse = console_parse_application;
679   gras_launch_application(NULL);
680   return 0;
681 }
682
683 static const luaL_Reg simgrid_funcs[] = {
684   {"create_environment", create_environment},
685   {"launch_application", launch_application},
686   {"debug", debug},
687   {"info", info},
688   {"run", run},
689   {"clean", clean},
690   /* short names */
691   {"platform", create_environment},
692   {"application", launch_application},
693   /* methods to bypass XML parser */
694   {"msg_register_platform", msg_register_platform},
695   {"sd_register_platform", sd_register_platform},
696   {"msg_register_application", msg_register_application},
697   {"gras_register_platform", gras_register_platform},
698   {"gras_register_application", gras_register_application},
699   /* gras sub generator method */
700   {"gras_set_process_function", gras_add_process_function},
701   {"gras_generate", gras_generate},
702   {NULL, NULL}
703 };
704
705 /* ********************************************************************************* */
706 /*                       module management functions                                 */
707 /* ********************************************************************************* */
708
709 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
710
711 int luaopen_simgrid(lua_State *L);     // Fuck gcc: we don't need that prototype
712
713 /**
714  * This function is called automatically by the Lua interpreter when some Lua code requires
715  * the "simgrid" module.
716  * @param L the Lua state
717  */
718 int luaopen_simgrid(lua_State *L)
719 {
720   XBT_DEBUG("luaopen_simgrid *****");
721
722   /* Get the command line arguments from the lua interpreter */
723   char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
724   int argc = 1;
725   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? */
726
727   lua_getglobal(L, "arg");
728   /* if arg is a null value, it means we use lua only as a script to init platform
729    * else it should be a table and then take arg in consideration
730    */
731   if (lua_istable(L, -1)) {
732     int done = 0;
733     while (!done) {
734       argc++;
735       lua_pushinteger(L, argc - 2);
736       lua_gettable(L, -2);
737       if (lua_isnil(L, -1)) {
738         done = 1;
739       } else {
740         xbt_assert(lua_isstring(L, -1),
741                     "argv[%d] got from lua is no string", argc - 1);
742         xbt_assert(argc < LUA_MAX_ARGS_COUNT,
743                     "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",
744                     __FILE__, LUA_MAX_ARGS_COUNT - 1);
745         argv[argc - 1] = (char *) luaL_checkstring(L, -1);
746         lua_pop(L, 1);
747         XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
748       }
749     }
750     argv[argc--] = NULL;
751
752     /* Initialize the MSG core */
753     MSG_global_init(&argc, argv);
754     XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
755   }
756
757   /* Keep the context mechanism informed of our lua world today */
758   sglua_maestro_state = L;
759
760   /* initialize access to my tables by children Lua states */
761   lua_newtable(L);
762   lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
763
764   register_c_functions(L);
765
766   return 1;
767 }
768
769 /**
770  * @brief Returns whether a Lua state is the maestro state.
771  * @param L a Lua state
772  * @return true if this is maestro
773  */
774 int sglua_is_maestro(lua_State* L) {
775   return L == sglua_maestro_state;
776 }
777
778 /**
779  * @brief Returns the maestro state.
780  * @return true the maestro Lua state
781  */
782 lua_State* sglua_get_maestro(void) {
783   return sglua_maestro_state;
784 }
785
786 /**
787  * Makes the appropriate Simgrid functions available to the Lua world.
788  * @param L a Lua world
789  */
790 void register_c_functions(lua_State *L) {
791
792   /* register the core C functions to lua */
793   luaL_register(L, "simgrid", simgrid_funcs);
794
795   /* register the task methods to lua */
796   luaL_openlib(L, TASK_MODULE_NAME, Task_methods, 0);   // create methods table, add it to the globals
797   luaL_newmetatable(L, TASK_MODULE_NAME);       // create metatable for Task, add it to the Lua registry
798   luaL_openlib(L, 0, Task_meta, 0);     // fill metatable
799   lua_pushliteral(L, "__index");
800   lua_pushvalue(L, -3);         // dup methods table
801   lua_rawset(L, -3);            // matatable.__index = methods
802   lua_pushliteral(L, "__metatable");
803   lua_pushvalue(L, -3);         // dup methods table
804   lua_rawset(L, -3);            // hide metatable:metatable.__metatable = methods
805   lua_pop(L, 1);                // drop metatable
806
807   /* register the hosts methods to lua */
808   luaL_openlib(L, HOST_MODULE_NAME, Host_methods, 0);
809   luaL_newmetatable(L, HOST_MODULE_NAME);
810   luaL_openlib(L, 0, Host_meta, 0);
811   lua_pushliteral(L, "__index");
812   lua_pushvalue(L, -3);
813   lua_rawset(L, -3);
814   lua_pushliteral(L, "__metatable");
815   lua_pushvalue(L, -3);
816   lua_rawset(L, -3);
817   lua_pop(L, 1);
818
819   /* register the links methods to lua */
820   luaL_openlib(L, AS_MODULE_NAME, AS_methods, 0);
821   luaL_newmetatable(L, AS_MODULE_NAME);
822   lua_pop(L, 1);
823
824   /* register the Tracing functions to lua */
825   luaL_openlib(L, TRACE_MODULE_NAME, Trace_methods, 0);
826   luaL_newmetatable(L, TRACE_MODULE_NAME);
827   lua_pop(L, 1);
828 }