2 #include <lua5.1/lauxlib.h>
3 #include <lua5.1/lualib.h>
8 #include "msg/datatypes.h"
9 #include "xbt/sysdep.h"
11 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua,bindings,"Lua Bindings");
18 =============================================
20 Example Lua Bindings for Msg
22 =============================================
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) {
40 int top = lua_gettop(L);
43 p+=sprintf(p,"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 p+=sprintf(p,"`%s'", lua_tostring(L, i));
52 case LUA_TBOOLEAN: /* booleans */
53 p+=sprintf(p,lua_toboolean(L, i) ? "true" : "false");
56 case LUA_TNUMBER: /* numbers */
57 p+=sprintf(p,"%g", lua_tonumber(L, i));
60 default: /* other values */
61 if ((ptr = luaL_checkudata(L,i,TASK))) {
64 p+=printf(p,"%s", lua_typename(L, t));
69 p+=sprintf(p," "); /* put a separator */
74 //**********************************************************TASK SECTION***************************************************
77 static Task toTask(lua_State *L,int index)
79 Task *pi = (Task*) lua_touserdata(L,index);
80 if(pi == NULL) luaL_typerror(L,index,TASK);
84 *checkTask ensures that a userdata on the stack is the correct type, and returns the Task pointer inside the userdata
87 static Task checkTask (lua_State *L,int index)
90 luaL_checktype(L,index,LUA_TUSERDATA);
91 pi = (Task*)luaL_checkudata(L,index,TASK);
92 if(pi == NULL ) luaL_typerror(L,index,TASK);
95 luaL_error(L,"null Task");
100 *pushTask leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata.
103 static Task *pushTask (lua_State *L,Task tk)
105 Task *pi = (Task*)lua_newuserdata(L,sizeof(Task));
107 luaL_getmetatable(L,TASK);
108 lua_setmetatable(L,-2);
114 *Task.new is a constructor that returns a userdata containing a pointer the Task to be manipulated
118 static int Task_new(lua_State* L)
120 const char *name=luaL_checkstring(L,1);
121 int comp_size = luaL_checkint(L,2);
122 int msg_size = luaL_checkint(L,3);
123 // We Will Set The Data as a Null for The Moment
124 pushTask(L,MSG_task_create(name,comp_size,msg_size,NULL));
125 INFO0("Created task");fflush(stdout);fflush(stderr);
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);
311 stackDump(L);fflush(stdout);fflush(stderr);
312 int res = MSG_task_send(tk,mailbox);
313 res++;//FIXME: check it instead of avoiding the warning
318 * Function Recieve a Task
320 static int Task_recv(lua_State *L) {
323 const char *mailbox = luaL_checkstring(L,1);
324 int res = MSG_task_receive(&tk,mailbox);
325 res++;//FIXME: check it instead of avoiding the warning
326 INFO1("Task Name : >>>%s",MSG_task_get_name(tk));
332 //******************************************************* PLATFORM & APPLICATION SECTION ****************************************************************
335 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WE WONT NEED IT (!!??)
337 * Launch Application Function
340 static int launch_application(lua_State *L)
343 const char * file = luaL_checkstring(L,1);
344 MSG_launch_application(file);
350 * Create Environment Function
353 static int create_environment(lua_State *L)
356 const char *file = luaL_checkstring(L,1);
357 MSG_create_environment(file);
363 //****************************************************************** REGISTERING ************************************************************
365 *Registering Into Lua
369 static const luaL_reg Task_methods[] = {
373 {"execute", Task_execute},
374 {"destroy", Task_destroy},
381 static const luaL_reg Host_methods[] = {
388 static const luaL_reg Process_methods[] = {
389 {"new", Process_new},
390 {"kill", Process_kill},
400 static int Task_gc(lua_State *L)
403 if (tk) MSG_task_destroy(tk);
404 //printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
408 static int Task_tostring(lua_State *L)
410 lua_pushfstring(L,"Task :%p",lua_touserdata(L,1));
415 static const luaL_reg Task_meta[] = {
417 {"__tostring", Task_tostring},
427 static int Host_gc(lua_State *L)
431 printf("GoodBye Host(%p)\n",lua_touserdata(L,1));
435 static int Host_tostring(lua_State *L)
437 lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
442 static const luaL_reg Host_meta[] = {
444 {"__tostring", Host_tostring},
452 static int Process_gc(lua_State *L)
454 Process ps=toProcess(L,1);
455 if (ps) MSG_process_kill(ps) ;
456 printf("GoodBye Process(%p)\n",lua_touserdata(L,1));
460 static int Process_tostring(lua_State *L)
462 lua_pushfstring(L,"Process :%p",lua_touserdata(L,1));
467 static const luaL_reg Process_meta[] = {
468 {"__gc", Process_gc},
469 {"__tostring", Process_tostring},
475 *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.
479 * Register Task, Host, and Process
482 int MSG_register(lua_State *L);// FIXME: better location
483 int MSG_register(lua_State *L) {
484 luaL_openlib(L,TASK,Task_methods,0); //create methods table,add it to the globals
485 luaL_newmetatable(L,TASK); //create metatable for Task,add it to the Lua registry
486 luaL_openlib(L,0,Task_meta,0);// fill metatable
487 lua_pushliteral(L,"__index");
488 lua_pushvalue(L,-3); //dup methods table
489 lua_rawset(L,-3); //matatable.__index = methods
490 lua_pushliteral(L,"__metatable");
491 lua_pushvalue(L,-3); //dup methods table
492 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
493 lua_pop(L,1); //drop metatable
495 luaL_openlib(L,HOST,Host_methods,0);
496 luaL_newmetatable(L,HOST);
497 luaL_openlib(L,0,Host_meta,0);
498 lua_pushliteral(L,"__index");
501 lua_pushliteral(L,"__metatable");
506 luaL_openlib(L,PROCESS,Process_methods,0);
507 luaL_newmetatable(L,PROCESS);
508 luaL_openlib(L,0,Host_meta,0);
509 lua_pushliteral(L,"__index");
512 lua_pushliteral(L,"__metatable");