Logo AND Algorithmique Numérique Distribuée

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