8 #include "msg/datatypes.h"
9 #include "xbt/sysdep.h"
11 #include "xbt/asserts.h"
17 =============================================
19 Example Lua Bindings for Msg
21 =============================================
24 XBT_LOG_NEW_DEFAULT_CATEGORY(lua,"Lua bindings");
27 #define TASK "Msg.Task"
28 #define HOST "Msg.Host"
29 #define PROCESS "Msg.Process"
31 typedef m_task_t Task;
32 typedef m_host_t Host;
33 typedef m_process_t Process;
36 static void stackDump (lua_State *L) {
38 int top = lua_gettop(L);
43 printf("STACK(top=%d): ",top);
44 for (i = 1; i <= top; i++) { /* repeat for each level */
45 int t = lua_type(L, i);
48 case LUA_TSTRING: /* strings */
49 printf("`%s'", lua_tostring(L, i));
52 case LUA_TBOOLEAN: /* booleans */
53 printf(lua_toboolean(L, i) ? "true" : "false");
56 case LUA_TNUMBER: /* numbers */
57 printf("%g", lua_tonumber(L, i));
60 default: /* other values */
61 if ((p = luaL_checkudata(L,i,TASK))) {
64 printf("%s", lua_typename(L, t));
69 printf(" "); /* put a separator */
71 printf("\n"); /* end the listing */
75 //**********************************************************TASK SECTION***************************************************
78 static Task toTask(lua_State *L,int index)
80 Task *pi = (Task*) lua_touserdata(L,index);
81 if(pi == NULL) luaL_typerror(L,index,TASK);
85 *checkTask ensures that a userdata on the stack is the correct type, and returns the Task pointer inside the userdata
88 static Task checkTask (lua_State *L,int index)
91 luaL_checktype(L,index,LUA_TUSERDATA);
92 pi = (Task*)luaL_checkudata(L,index,TASK);
93 if(pi == NULL ) luaL_typerror(L,index,TASK);
96 luaL_error(L,"null Task");
101 *pushTask leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata.
104 static Task *pushTask (lua_State *L,Task tk)
106 Task *pi = (Task*)lua_newuserdata(L,sizeof(Task));
108 luaL_getmetatable(L,TASK);
109 lua_setmetatable(L,-2);
115 *Task.new is a constructor that returns a userdata containing a pointer the Task to be manipulated
119 static int Task_new(lua_State* L)
121 const char *name=luaL_checkstring(L,1);
122 int comp_size = luaL_checkint(L,2);
123 int msg_size = luaL_checkint(L,3);
124 // We Will Set The Data as a Null for The Moment
125 pushTask(L,MSG_task_create(name,comp_size,msg_size,NULL));
131 * Function to return the Task Name
135 static int Task_name(lua_State *L)
137 Task tk = checkTask(L,1);
138 lua_pushstring(L,MSG_task_get_name(tk));
144 * Function to return the Computing Size of a Task
147 static int Task_comp(lua_State *L)
149 Task tk = checkTask(L,1);
150 lua_pushnumber(L,MSG_task_get_compute_duration (tk));
156 *Function to Excute a Task
159 static int Task_execute(lua_State *L)
161 Task tk = checkTask(L,1);
162 int res = MSG_task_execute(tk);
163 lua_pushnumber(L,res);
168 *Function ro Desroy a Task
170 static int Task_destroy(lua_State *L)
172 Task tk = checkTask(L,1);
173 int res = MSG_task_destroy(tk);
174 lua_pushnumber(L,res);
183 //*************************************************************** HOST SECTION ***************************************************************************
187 static Host toHost(lua_State *L,int index)
189 Host *pi = (Host*) lua_touserdata(L,index);
190 if(pi == NULL) luaL_typerror(L,index,HOST);
196 static Host checkHost (lua_State *L,int index)
199 luaL_checktype(L,index,LUA_TUSERDATA);
200 pi = (Host*)luaL_checkudata(L,index,HOST);
201 if(pi == NULL ) luaL_typerror(L,index,HOST);
204 luaL_error(L,"null Host");
210 static Host *pushHost (lua_State *L,Host ht)
212 Host *pi = (Host*)lua_newuserdata(L,sizeof(Host));
214 luaL_getmetatable(L,HOST);
215 lua_setmetatable(L,-2);
221 static int Host_new(lua_State* L)
223 const char *host_name=luaL_checkstring(L,1);
224 pushHost(L,MSG_get_host_by_name(host_name));
230 static int Host_name(lua_State* L)
232 Host ht = checkHost(L,1);
233 lua_pushstring(L,MSG_host_get_name(ht));
238 //******************************************************* PROCESS SECTION ***************************************************************************
239 static Process toProcess(lua_State *L,int index)
241 Process *pi = (Process*) lua_touserdata(L,index);
242 if(pi == NULL) luaL_typerror(L,index,PROCESS);
248 static Process checkProcess (lua_State *L,int index)
251 luaL_checktype(L,index,LUA_TUSERDATA);
252 pi = (Process*)luaL_checkudata(L,index,PROCESS);
253 if(pi == NULL ) luaL_typerror(L,index,PROCESS);
256 luaL_error(L,"null Process");
262 static Process *pushProcess (lua_State *L,Process ps)
264 Process *pi = (Process*)lua_newuserdata(L,sizeof(Process));
266 luaL_getmetatable(L,PROCESS);
267 lua_setmetatable(L,-2);
273 m_process_t MSG_process_create ( const char * name,
274 xbt_main_func_t code,
282 static int Process_new(lua_State* L)
284 const char *name=luaL_checkstring(L,1);
285 //int code = luaL_checkint(L,2);
286 //xbt_main_func_t << code
287 // We Will Set The Data as a Null for The Moment
288 Host ht = checkHost(L,4);
289 pushProcess(L,MSG_process_create(name,NULL,NULL,ht));
295 static int Process_kill(lua_State *L)
297 Process ps = checkProcess (L,1);
298 MSG_process_kill(ps);
304 //**********************************************************MSG Operating System Functions******************************************************************
306 * Function to Send a Task
308 static int Task_send(lua_State *L) {
309 Task tk = checkTask(L,1);
310 const char *mailbox = luaL_checkstring(L,2);
312 int res = MSG_task_send(tk,mailbox);
317 * Function Recieve a Task
319 static int Task_recv2(lua_State *L) {
321 Task *tk = (Task *)checkTask(L,1);
322 const char *mailbox = luaL_checkstring(L,2);
323 MSG_task_receive(tk,mailbox);
324 printf("Task Name Within Receive Fct: >>>%s\n",MSG_task_get_name(*tk));
330 static int Task_recv(lua_State *L) {
333 const char *mailbox = luaL_checkstring(L,1);
334 int res = MSG_task_receive(&tk,mailbox);
335 INFO1("Task Name : >>>%s",MSG_task_get_name(tk));
341 //******************************************************* PLATFORM & APPLICATION SECTION ****************************************************************
344 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WE WONT NEED IT (!!??)
346 * Launch Application Function
349 static int launch_application(lua_State *L)
352 const char * file = luaL_checkstring(L,1);
353 MSG_launch_application(file);
359 * Create Environment Function
362 static int create_environment(lua_State *L)
365 const char *file = luaL_checkstring(L,1);
366 MSG_create_environment(file);
375 static int function_register()
382 //****************************************************************** REGISTERING ************************************************************
384 *Registering Into Lua
388 static const luaL_reg Task_methods[] = {
392 {"execute", Task_execute},
393 {"destroy", Task_destroy},
396 {"recv2", Task_recv2},
401 static const luaL_reg Host_methods[] = {
408 static const luaL_reg Process_methods[] = {
409 {"new", Process_new},
410 {"kill", Process_kill},
420 static int Task_gc(lua_State *L)
423 //if (tk) MSG_task_destroy(tk);
424 //printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
428 static int Task_tostring(lua_State *L)
430 lua_pushfstring(L,"Task :%p",lua_touserdata(L,1));
435 static const luaL_reg Task_meta[] = {
437 {"__tostring", Task_tostring},
447 static int Host_gc(lua_State *L)
451 printf("GoodBye Host(%p)\n",lua_touserdata(L,1));
455 static int Host_tostring(lua_State *L)
457 lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
462 static const luaL_reg Host_meta[] = {
464 {"__tostring", Host_tostring},
472 static int Process_gc(lua_State *L)
474 Process ps=toProcess(L,1);
475 if (ps) MSG_process_kill(ps) ;
476 printf("GoodBye Process(%p)\n",lua_touserdata(L,1));
480 static int Process_tostring(lua_State *L)
482 lua_pushfstring(L,"Process :%p",lua_touserdata(L,1));
487 static const luaL_reg Process_meta[] = {
488 {"__gc", Process_gc},
489 {"__tostring", Process_tostring},
495 *The metatable for the userdata is put in the registry, and the __index field points to the table of methods so that the object:method() syntax will work. The methods table is stored in the table of globals so that scripts can add methods written in Lua.
502 int Task_register(lua_State *L)
504 luaL_openlib(L,TASK,Task_methods,0); //create methods table,add it to the globals
505 luaL_newmetatable(L,TASK); //create metatable for Task,add it to the Lua registry
506 luaL_openlib(L,0,Task_meta,0);// fill metatable
507 lua_pushliteral(L,"__index");
508 lua_pushvalue(L,-3); //dup methods table
509 lua_rawset(L,-3); //matatable.__index = methods
510 lua_pushliteral(L,"__metatable");
511 lua_pushvalue(L,-3); //dup methods table
512 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
513 lua_pop(L,1); //drop metatable
521 int Host_register(lua_State *L)
523 luaL_openlib(L,HOST,Host_methods,0);
524 luaL_newmetatable(L,HOST);
525 luaL_openlib(L,0,Host_meta,0);
526 lua_pushliteral(L,"__index");
529 lua_pushliteral(L,"__metatable");
540 int Process_register(lua_State *L)
542 luaL_openlib(L,PROCESS,Process_methods,0);
543 luaL_newmetatable(L,PROCESS);
544 luaL_openlib(L,0,Host_meta,0);
545 lua_pushliteral(L,"__index");
548 lua_pushliteral(L,"__metatable");