Logo AND Algorithmique Numérique Distribuée

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