Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unused code, take into consideration in the luaopen_simgrid when lua is used...
[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
9 #include <stdio.h>
10 #include <lauxlib.h>
11 #include <lualib.h>
12 #include "msg/msg.h"
13 #include "xbt.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua,bindings,"Lua Bindings");
16
17 #define TASK_MODULE_NAME "simgrid.Task"
18 #define HOST_MODULE_NAME "simgrid.Host"
19 // Surf ( bypass XML )
20 #define LINK_MODULE_NAME "simgrid.Link"
21 #define ROUTE_MODULE_NAME "simgrid.Route"
22
23 /* ********************************************************************************* */
24 /*                            helper functions                                       */
25 /* ********************************************************************************* */
26 static void stackDump (const char *msg, lua_State *L) {
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   INFO2("%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   m_task_t *pi,tk;
73   luaL_checktype(L,index,LUA_TTABLE);
74   lua_getfield(L,index,"__simgrid_task");
75   pi = (m_task_t*)luaL_checkudata(L,-1,TASK_MODULE_NAME);
76   if(pi == NULL)
77          luaL_typerror(L,index,TASK_MODULE_NAME);
78   tk = *pi;
79   if(!tk)
80          luaL_error(L,"null Task");
81   lua_pop(L,1);
82   return  tk;
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           const char *name=luaL_checkstring(L,1);
113           int comp_size = luaL_checkint(L,2);
114           int msg_size = luaL_checkint(L,3);
115           m_task_t msg_task = MSG_task_create(name,comp_size,msg_size,NULL);
116           lua_newtable (L); /* create a table, put the userdata on top of it */
117           m_task_t *lua_task = (m_task_t*)lua_newuserdata(L,sizeof(m_task_t));
118           *lua_task = msg_task;
119           luaL_getmetatable(L,TASK_MODULE_NAME);
120           lua_setmetatable(L,-2);
121           lua_setfield (L, -2, "__simgrid_task"); /* put the userdata as field of the table */
122           /* remove the args from the stack */
123           lua_remove(L,1);
124           lua_remove(L,1);
125           lua_remove(L,1);
126           return 1;
127 }
128
129 static int Task_get_name(lua_State *L) {
130   m_task_t tk = checkTask(L,-1);
131   lua_pushstring(L,MSG_task_get_name(tk));
132   return 1;
133 }
134
135 static int Task_computation_duration(lua_State *L){
136   m_task_t tk = checkTask(L,-1);
137   lua_pushnumber(L,MSG_task_get_compute_duration (tk));
138   return 1;
139 }
140
141 static int Task_execute(lua_State *L){
142   m_task_t tk = checkTask(L,-1);
143   int res = MSG_task_execute(tk);
144   lua_pushnumber(L,res);
145   return 1;
146 }
147
148 static int Task_destroy(lua_State *L) {
149   m_task_t tk = checkTask(L,-1);
150   int res = MSG_task_destroy(tk);
151   lua_pushnumber(L,res);
152   return 1;
153 }
154
155 static int Task_send(lua_State *L)  {
156   //stackDump("send ",L);
157   m_task_t tk = checkTask(L,-2);
158   const char *mailbox = luaL_checkstring(L,-1);
159   lua_pop(L,1); // remove the string so that the task is on top of it
160   MSG_task_set_data(tk,L); // Copy my stack into the task, so that the receiver can copy the lua task directly
161   MSG_error_t res = MSG_task_send(tk,mailbox);
162   while (MSG_task_get_data(tk)!=NULL) // Don't mess up with my stack: the receiver didn't copy the data yet
163     MSG_process_sleep(0); // yield
164
165   if (res != MSG_OK) switch(res) {
166     case MSG_TIMEOUT :
167       ERROR0("MSG_task_send failed : Timeout");
168       break;
169     case MSG_TRANSFER_FAILURE :
170       ERROR0("MSG_task_send failed : Transfer Failure");
171       break;
172     case MSG_HOST_FAILURE :
173       ERROR0("MSG_task_send failed : Host Failure ");
174       break;
175     default :
176       ERROR0("MSG_task_send failed : Unexpected error , please report this bug");
177       break;
178     }
179   return 0;
180 }
181
182 static int Task_recv(lua_State *L)  {
183   m_task_t tk = NULL;
184   const char *mailbox = luaL_checkstring(L,-1);
185   MSG_error_t res = MSG_task_receive(&tk,mailbox);
186
187   lua_State *sender_stack = MSG_task_get_data(tk);
188   lua_xmove(sender_stack,L,1); // copy the data directly from sender's stack
189   MSG_task_set_data(tk,NULL);
190
191   if(res != MSG_OK) switch(res){
192           case MSG_TIMEOUT :
193                   ERROR0("MSG_task_receive failed : Timeout");
194                   break;
195           case MSG_TRANSFER_FAILURE :
196                   ERROR0("MSG_task_receive failed : Transfer Failure");
197                   break;
198           case MSG_HOST_FAILURE :
199                   ERROR0("MSG_task_receive failed : Host Failure ");
200                   break;
201           default :
202                   ERROR0("MSG_task_receive failed : Unexpected error , please report this bug");
203                   break;
204                   }
205
206   return 1;
207 }
208
209 static const luaL_reg Task_methods[] = {
210     {"new",   Task_new},
211     {"name",  Task_get_name},
212     {"computation_duration",  Task_computation_duration},
213     {"execute", Task_execute},
214     {"destroy", Task_destroy},
215     {"send",    Task_send},
216     {"recv",    Task_recv},
217     {0,0}
218 };
219 static int Task_gc(lua_State *L) {
220   m_task_t tk=checkTask(L,-1);
221   if (tk) MSG_task_destroy(tk);
222   return 0;
223 }
224
225 static int Task_tostring(lua_State *L) {
226   lua_pushfstring(L, "Task :%p",lua_touserdata(L,1));
227   return 1;
228 }
229
230 static const luaL_reg Task_meta[] = {
231     {"__gc",  Task_gc},
232     {"__tostring",  Task_tostring},
233     {0,0}
234 };
235
236 /**
237  * Host
238  */
239 static m_host_t checkHost (lua_State *L,int index) {
240   m_host_t *pi,ht;
241   luaL_checktype(L,index,LUA_TTABLE);
242   lua_getfield(L,index,"__simgrid_host");
243   pi = (m_host_t*)luaL_checkudata(L,-1,HOST_MODULE_NAME);
244   if(pi == NULL)
245          luaL_typerror(L,index,HOST_MODULE_NAME);
246   ht = *pi;
247   if(!ht)
248          luaL_error(L,"null Host");
249   lua_pop(L,1);
250   return  ht;
251 }
252
253 static int Host_get_by_name(lua_State *L)
254 {
255         const char *name=luaL_checkstring(L,1);
256         DEBUG0("Getting Host from name...");
257         m_host_t msg_host = MSG_get_host_by_name(name);
258         if (!msg_host)
259                 {
260                 luaL_error(L,"null Host : MSG_get_host_by_name failled");
261                 }
262     lua_newtable (L); /* create a table, put the userdata on top of it */
263         m_host_t *lua_host = (m_host_t*)lua_newuserdata(L,sizeof(m_host_t));
264         *lua_host = msg_host;
265         luaL_getmetatable(L,HOST_MODULE_NAME);
266         lua_setmetatable(L,-2);
267         lua_setfield (L, -2, "__simgrid_host"); /* put the userdata as field of the table */
268         /* remove the args from the stack */
269         lua_remove(L,1);
270         return 1;
271 }
272
273
274 static int Host_get_name(lua_State *L) {
275   m_host_t ht = checkHost(L,-1);
276   lua_pushstring(L,MSG_host_get_name(ht));
277   return 1;
278 }
279
280 static int Host_number(lua_State *L) {
281   lua_pushnumber(L,MSG_get_host_number());
282   return 1;
283 }
284
285 static int Host_at(lua_State *L)
286 {
287         int index = luaL_checkinteger(L,1);
288         m_host_t host = MSG_get_host_table()[index-1]; // lua indexing start by 1 (lua[1] <=> C[0])
289         lua_newtable (L); /* create a table, put the userdata on top of it */
290         m_host_t *lua_host = (m_host_t*)lua_newuserdata(L,sizeof(m_host_t));
291         *lua_host = host;
292         luaL_getmetatable(L,HOST_MODULE_NAME);
293         lua_setmetatable(L,-2);
294         lua_setfield (L, -2, "__simgrid_host"); /* put the userdata as field of the table */
295         return 1;
296
297 }
298
299 /*****************************************************************************************
300                                                              * BYPASS XML SURF Methods *
301                                                                  ***************************
302                                                                  ***************************
303 ******************************************************************************************/
304 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
305 #include "surf/surf_private.h"
306 typedef struct t_host_attr
307 {
308         //platform attribute
309         // Mandatory attributes
310         const char* id;
311         double power_peak;
312         // Optional attributes
313         double power_scale;
314         const char *power_trace;
315         int state_initial;
316         const char *state_trace;
317         //deployment attribute
318         const char* function;
319         xbt_dynar_t args_list;
320 }host_attr,*p_host_attr;
321
322 typedef struct t_link_attr
323 {
324         const char* id;
325         double bandwidth;
326         double latency;
327 }link_attr,*p_link_attr;
328
329 typedef struct t_route_attr
330 {
331         const char *src_id;
332         const char *dest_id;
333         xbt_dynar_t links_id;
334
335 }route_attr,*p_route_attr;
336
337 //using xbt_dynar_t :
338 static xbt_dynar_t host_list_d ;
339 static xbt_dynar_t link_list_d ;
340 static xbt_dynar_t route_list_d ;
341
342
343 //create resource
344
345 static void create_host(const char* id,double power_peak,double power_sc,
346                                                 const char* power_tr,int state_init,
347                                                 const char* state_tr)
348 {
349
350         double power_scale = 1.0;
351         tmgr_trace_t power_trace = NULL;
352         e_surf_resource_state_t state_initial;
353         tmgr_trace_t state_trace;
354         if(power_sc) // !=0
355                 power_scale = power_sc;
356         if (state_init == -1)
357                 state_initial = SURF_RESOURCE_OFF;
358         else
359                 state_initial = SURF_RESOURCE_ON;
360         if(power_tr)
361                 power_trace = tmgr_trace_new(power_tr);
362         else
363                 power_trace = tmgr_trace_new("");
364         if(state_tr)
365                 state_trace = tmgr_trace_new(state_tr);
366         else
367                 state_trace = tmgr_trace_new("");
368         current_property_set = xbt_dict_new();
369         surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
370                                                power_trace, state_initial, state_trace, current_property_set);
371
372 }
373
374 static int Host_new(lua_State *L)
375 {
376
377         if(xbt_dynar_is_empty(host_list_d))
378                 host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
379
380         p_host_attr host;
381         const char * id;
382         const char *power_trace;
383         const char *state_trace;
384         double power,power_scale;
385         int state_initial;
386         //get values from the table passed as argument
387     if (lua_istable(L,-1)) {
388
389             // get Id Value
390             lua_pushstring(L,"id");
391             lua_gettable(L, -2 );
392             id = lua_tostring(L,-1);
393             lua_pop(L,1);
394
395             // get power value
396             lua_pushstring(L,"power");
397             lua_gettable(L, -2 );
398             power = lua_tonumber(L,-1);
399             lua_pop(L,1);
400
401             //get power_scale
402             lua_pushstring(L,"power_scale");
403             lua_gettable(L, -2 );
404             power_scale = lua_tonumber(L,-1);
405             lua_pop(L,1);
406
407             //get power_trace
408             lua_pushstring(L,"power_trace");
409             lua_gettable(L, -2 );
410             power_trace = lua_tostring(L,-1);
411             lua_pop(L,1);
412
413             //get state initial
414             lua_pushstring(L,"state_initial");
415             lua_gettable(L, -2 );
416             state_initial = lua_tonumber(L,-1);
417             lua_pop(L,1);
418
419             //get trace state
420             lua_pushstring(L,"state_trace");
421             lua_gettable(L, -2 );
422             state_trace = lua_tostring(L,-1);
423             lua_pop(L,1);
424
425     } else {
426             ERROR0("Bad Arguments to create host, Should be a table with named arguments");
427             return -1;
428     }
429
430             host = malloc(sizeof(host_attr));
431                 host->id = id;
432                 host->power_peak = power;
433                 host->power_scale = power_scale;
434                 host->power_trace = power_trace;
435                 host->state_initial = state_initial;
436                 host->state_trace = state_trace;
437                 host->function = NULL;
438                 xbt_dynar_push(host_list_d, &host);
439
440     return 0;
441 }
442
443 static int Link_new(lua_State *L) // (id,bandwidth,latency)
444 {
445         if(xbt_dynar_is_empty(link_list_d))
446                 link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
447
448         const char* id;
449         double bandwidth,latency;
450         //get values from the table passed as argument
451         if (lua_istable(L,-1)) {
452                     // get Id Value
453                     lua_pushstring(L,"id");
454                     lua_gettable(L, -2 );
455                     id = lua_tostring(L,-1);
456                     lua_pop(L,1);
457
458                     // get bandwidth value
459                     lua_pushstring(L,"bandwidth");
460                     lua_gettable(L, -2 );
461                     bandwidth = lua_tonumber(L,-1);
462                     lua_pop(L,1);
463
464                     //get latency value
465                     lua_pushstring(L,"latency");
466                     lua_gettable(L, -2 );
467                     latency = lua_tonumber(L,-1);
468                     lua_pop(L,1);
469
470             } else {
471                     ERROR0("Bad Arguments to create link, Should be a table with named arguments");
472                     return -1;
473             }
474
475         p_link_attr link = malloc(sizeof(link_attr));
476         link->id = id;
477         link->bandwidth = bandwidth;
478         link->latency = latency;
479         xbt_dynar_push(link_list_d,&link);
480         return 0;
481 }
482
483 static int Route_new(lua_State *L) // (src_id,dest_id,links_number,link_table)
484 {
485         if(xbt_dynar_is_empty(route_list_d))
486                 route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
487         const char * link_id;
488         p_route_attr route = malloc(sizeof(route_attr));
489         route->src_id = luaL_checkstring(L,1);
490         route->dest_id = luaL_checkstring(L,2);
491         route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
492         lua_pushnil(L);
493         while (lua_next(L,3) != 0) {
494                 link_id = lua_tostring(L, -1);
495                 xbt_dynar_push(route->links_id, &link_id);
496             DEBUG2("index = %f , Link_id = %s \n",lua_tonumber(L, -2),lua_tostring(L, -1));
497             lua_pop(L, 1);
498         }
499         lua_pop(L, 1);
500
501         //add route to platform's route list
502         xbt_dynar_push(route_list_d,&route);
503         return 0;
504 }
505
506 static int Host_set_function(lua_State *L) //(host,function,nb_args,list_args)
507 {
508         // look for the index of host in host_list
509         const char *host_id = luaL_checkstring(L,1);
510         const char* argument;
511         unsigned int i;
512         p_host_attr p_host;
513
514         xbt_dynar_foreach(host_list_d,i,p_host)
515         {
516                 if(p_host->id == host_id)
517                 {
518                         p_host->function = luaL_checkstring(L,2);
519                         p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
520                         // fill the args list
521                         lua_pushnil(L);
522                         int j = 0;
523                         while (lua_next(L,3) != 0) {
524                                         argument = lua_tostring(L, -1);
525                                         xbt_dynar_push(p_host->args_list, &argument);
526                                     DEBUG2("index = %f , Arg_id = %s \n",lua_tonumber(L, -2),lua_tostring(L, -1));
527                                     j++;
528                                     lua_pop(L, 1);
529                                 }
530                         lua_pop(L, 1);
531                         return 0;
532                 }
533         }
534         ERROR1("Host : %s Not Fount !!",host_id);
535         return 1;
536 }
537
538 /*
539  * surf parse bypass platform
540  */
541 static int surf_parse_bypass_platform()
542 {
543         unsigned int i;
544         p_host_attr p_host;
545         p_link_attr p_link;
546         p_route_attr p_route;
547
548         // Add Hosts
549         xbt_dynar_foreach(host_list_d,i,p_host)
550         {
551                 create_host(p_host->id,p_host->power_peak,p_host->power_scale,p_host->power_trace,
552                                         p_host->state_initial,p_host->state_trace);
553                 //add to routing model host list
554                 surf_route_add_host((char*)p_host->id);
555         }
556
557         //add Links
558         xbt_dynar_foreach(link_list_d,i,p_link)
559         {
560                 surf_link_create_resouce((char*)p_link->id,p_link->bandwidth,p_link->latency);
561         }
562         // add route
563         xbt_dynar_foreach(route_list_d,i,p_route)
564         {
565                 surf_route_set_resource((char*)p_route->src_id,(char*)p_route->dest_id,p_route->links_id,0);
566         }
567         /* </platform> */
568
569         surf_add_host_traces();
570         surf_set_routes();
571         surf_add_link_traces();
572
573         return 0; // must return 0 ?!!
574
575 }
576 /*
577  * surf parse bypass application
578  */
579 static int surf_parse_bypass_application()
580 {
581           unsigned int i;
582           p_host_attr p_host;
583           xbt_dynar_foreach(host_list_d,i,p_host)
584                   {
585                   if(p_host->function)
586                           MSG_set_function(p_host->id,p_host->function,p_host->args_list);
587                   }
588           return 0;
589 }
590
591 //***********Register Methods *******************************************//
592 /*
593  * Host Methods
594  */
595 static const luaL_reg Host_methods[] = {
596     {"getByName",   Host_get_by_name},
597     {"name",            Host_get_name},
598     {"number",          Host_number},
599     {"at",                      Host_at},
600     // Bypass XML Methods
601     {"new",                     Host_new},
602     {"setFunction",     Host_set_function},
603     {0,0}
604 };
605
606 static int Host_gc(lua_State *L)
607 {
608   m_host_t ht = checkHost(L,-1);
609   if (ht) ht = NULL;
610   return 0;
611 }
612
613 static int Host_tostring(lua_State *L)
614 {
615   lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
616   return 1;
617 }
618
619 static const luaL_reg Host_meta[] = {
620     {"__gc",  Host_gc},
621     {"__tostring",  Host_tostring},
622     {0,0}
623 };
624
625 /*
626  * Link Methods
627  */
628 static const luaL_reg Link_methods[] = {
629     {"new",Link_new},
630     {0,0}
631 };
632 /*
633  * Route Methods
634  */
635 static const luaL_reg Route_methods[] ={
636    {"new",Route_new},
637    {0,0}
638 };
639
640 /*
641  * Environment related
642  */
643
644 extern lua_State *simgrid_lua_state;
645
646 static int run_lua_code(int argc,char **argv) {
647   DEBUG1("Run lua code %s",argv[0]);
648   lua_State *L = lua_newthread(simgrid_lua_state);
649   int ref = luaL_ref(simgrid_lua_state, LUA_REGISTRYINDEX); // protect the thread from being garbage collected
650   int res = 1;
651
652   /* Start the co-routine */
653   lua_getglobal(L,argv[0]);
654   xbt_assert1(lua_isfunction(L,-1),
655       "The lua function %s does not seem to exist",argv[0]);
656
657   // push arguments onto the stack
658   int i;
659   for(i=1;i<argc;i++)
660     lua_pushstring(L,argv[i]);
661
662   // Call the function (in resume)
663   xbt_assert2(lua_pcall(L, argc-1, 1, 0) == 0,
664     "error running function `%s': %s",argv[0], lua_tostring(L, -1));
665
666   /* retrieve result */
667   if (lua_isnumber(L, -1)) {
668     res = lua_tonumber(L, -1);
669     lua_pop(L, 1);  /* pop returned value */
670   }
671   // cleanups
672   luaL_unref(simgrid_lua_state,LUA_REGISTRYINDEX,ref );
673   DEBUG1("Execution of lua code %s is over", (argv ? argv[0] : "(null)"));
674   return res;
675 }
676 static int launch_application(lua_State *L) {
677   const char * file = luaL_checkstring(L,1);
678   MSG_function_register_default(run_lua_code);
679   MSG_launch_application(file);
680   return 0;
681 }
682 #include "simix/simix.h" //FIXME: KILLME when debugging on simix internals become useless
683 static int create_environment(lua_State *L) {
684   const char *file = luaL_checkstring(L,1);
685   DEBUG1("Loading environment file %s",file);
686   MSG_create_environment(file);
687   smx_host_t *hosts = SIMIX_host_get_table();
688   int i;
689   for (i=0;i<SIMIX_host_get_number();i++) {
690     DEBUG1("We have an host %s", SIMIX_host_get_name(hosts[i]));
691   }
692
693   return 0;
694 }
695
696 static int debug(lua_State *L) {
697   const char *str = luaL_checkstring(L,1);
698   DEBUG1("%s",str);
699   return 0;
700 }
701 static int info(lua_State *L) {
702   const char *str = luaL_checkstring(L,1);
703   INFO1("%s",str);
704   return 0;
705 }
706 static int run(lua_State *L) {
707   MSG_main();
708   return 0;
709 }
710 static int clean(lua_State *L) {
711   MSG_clean();
712   return 0;
713 }
714
715 /*
716  * Bypass XML Parser
717  */
718 static int register_platform(lua_State *L)
719 {
720         /* Tell Simgrid we dont wanna use its parser*/
721         surf_parse = surf_parse_bypass_platform;
722         MSG_create_environment(NULL);
723         return 0;
724 }
725
726 static int register_application(lua_State *L)
727 {
728          MSG_function_register_default(run_lua_code);
729          surf_parse = surf_parse_bypass_application;
730          MSG_launch_application(NULL);
731          return 0;
732 }
733
734 static const luaL_Reg simgrid_funcs[] = {
735     { "create_environment", create_environment},
736     { "launch_application", launch_application},
737     { "debug", debug},
738     { "info", info},
739     { "run", run},
740     { "clean", clean},
741     /* short names */
742     { "platform", create_environment},
743     { "application", launch_application},
744     /* methods to bypass XML parser*/
745     { "register_platform",register_platform},
746     { "register_application",register_application},
747     { NULL, NULL }
748 };
749
750 /* ********************************************************************************* */
751 /*                       module management functions                                 */
752 /* ********************************************************************************* */
753
754 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
755
756 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
757 #define TEST
758 int luaopen_simgrid(lua_State* L); // Fuck gcc: we don't need that prototype
759 int luaopen_simgrid(lua_State* L) {
760
761   //xbt_ctx_factory_to_use = "lua";
762   char **argv=malloc(sizeof(char*)*LUA_MAX_ARGS_COUNT);
763   int argc=1;
764   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? */
765   /* Get the command line arguments from the lua interpreter */
766   lua_getglobal(L,"arg");
767   /* if arg is a null value, it means we use lua only as a script to init platform
768    * else it should be a table and then take arg in consideration
769    */
770   if(lua_istable(L,-1))
771   {
772           int done=0;
773           while (!done) {
774                 argc++;
775                 lua_pushinteger(L,argc-2);
776                 lua_gettable(L,-2);
777                 if (lua_isnil(L,-1)) {
778                   done = 1;
779                 } else {
780                   xbt_assert1(lua_isstring(L,-1),"argv[%d] got from lua is no string",argc-1);
781                   xbt_assert2(argc<LUA_MAX_ARGS_COUNT,
782                            "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",
783                            __FILE__,LUA_MAX_ARGS_COUNT-1);
784                   argv[argc-1] = (char*)luaL_checkstring(L,-1);
785                   lua_pop(L,1);
786                   DEBUG1("Got command line argument %s from lua",argv[argc-1]);
787                 }
788           }
789           argv[argc--]=NULL;
790
791           /* Initialize the MSG core */
792           MSG_global_init(&argc,argv);
793           DEBUG1("Still %d arguments on command line",argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
794  }
795   /* register the core C functions to lua */
796   luaL_register(L, "simgrid", simgrid_funcs);
797   /* register the task methods to lua */
798   luaL_openlib(L,TASK_MODULE_NAME,Task_methods,0); //create methods table,add it to the globals
799   luaL_newmetatable(L,TASK_MODULE_NAME); //create metatable for Task,add it to the Lua registry
800   luaL_openlib(L,0,Task_meta,0);// fill metatable
801   lua_pushliteral(L,"__index");
802   lua_pushvalue(L,-3);  //dup methods table
803   lua_rawset(L,-3); //matatable.__index = methods
804   lua_pushliteral(L,"__metatable");
805   lua_pushvalue(L,-3);  //dup methods table
806   lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
807   lua_pop(L,1);   //drop metatable
808
809   /* register the hosts methods to lua*/
810   luaL_openlib(L,HOST_MODULE_NAME,Host_methods,0);
811   luaL_newmetatable(L,HOST_MODULE_NAME);
812   luaL_openlib(L,0,Host_meta,0);
813   lua_pushliteral(L,"__index");
814   lua_pushvalue(L,-3);
815   lua_rawset(L,-3);
816   lua_pushliteral(L,"__metatable");
817   lua_pushvalue(L,-3);
818   lua_rawset(L,-3);
819   lua_pop(L,1);
820
821   /* register the links methods to lua*/
822   luaL_openlib(L,LINK_MODULE_NAME,Link_methods,0);
823   luaL_newmetatable(L,LINK_MODULE_NAME);
824   lua_pop(L,1);
825
826   /*register the routes methods to lua*/
827   luaL_openlib(L,ROUTE_MODULE_NAME,Route_methods,0);
828   luaL_newmetatable(L,LINK_MODULE_NAME);
829   lua_pop(L,1);
830
831   /* Keep the context mechanism informed of our lua world today */
832   simgrid_lua_state = L;
833   return 1;
834 }