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"
17 =============================================
19 Example Lua Bindings for Msg
21 =============================================
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smx_lua,simix,
26 "Messages specific for this lua example");
29 #define TASK "Msg.Task"
30 #define HOST "Msg.Host"
31 #define PROCESS "Msg.Process"
33 typedef m_task_t Task;
34 typedef m_host_t Host;
35 typedef m_process_t Process;
38 static void stackDump (lua_State *L) {
42 int top = lua_gettop(L);
45 p+=sprintf(p,"STACK(top=%d): ",top);
46 for (i = 1; i <= top; i++) { /* repeat for each level */
47 int t = lua_type(L, i);
50 case LUA_TSTRING: /* strings */
51 p+=sprintf(p,"`%s'", lua_tostring(L, i));
54 case LUA_TBOOLEAN: /* booleans */
55 p+=sprintf(p,lua_toboolean(L, i) ? "true" : "false");
58 case LUA_TNUMBER: /* numbers */
59 p+=sprintf(p,"%g", lua_tonumber(L, i));
62 default: /* other values */
63 if ((ptr = luaL_checkudata(L,i,TASK))) {
66 p+=printf(p,"%s", lua_typename(L, t));
71 p+=sprintf(p," "); /* put a separator */
76 //**********************************************************TASK SECTION***************************************************
79 static Task toTask(lua_State *L,int index)
81 Task *pi = (Task*) lua_touserdata(L,index);
82 if(pi == NULL) luaL_typerror(L,index,TASK);
86 *checkTask ensures that a userdata on the stack is the correct type, and returns the Task pointer inside the userdata
89 static Task checkTask (lua_State *L,int index)
92 luaL_checktype(L,index,LUA_TUSERDATA);
93 pi = (Task*)luaL_checkudata(L,index,TASK);
94 if(pi == NULL ) luaL_typerror(L,index,TASK);
97 luaL_error(L,"null Task");
102 *pushTask leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata.
105 static Task *pushTask (lua_State *L,Task tk)
107 Task *pi = (Task*)lua_newuserdata(L,sizeof(Task));
109 luaL_getmetatable(L,TASK);
110 lua_setmetatable(L,-2);
116 *Task.new is a constructor that returns a userdata containing a pointer the Task to be manipulated
120 static int Task_new(lua_State* L)
122 const char *name=luaL_checkstring(L,1);
123 int comp_size = luaL_checkint(L,2);
124 int msg_size = luaL_checkint(L,3);
125 // We Will Set The Data as a Null for The Moment
126 pushTask(L,MSG_task_create(name,comp_size,msg_size,NULL));
127 INFO0("Created task");fflush(stdout);fflush(stderr);
133 * Function to return the Task Name
137 static int Task_name(lua_State *L)
139 Task tk = checkTask(L,1);
140 lua_pushstring(L,MSG_task_get_name(tk));
146 * Function to return the Computing Size of a Task
149 static int Task_comp(lua_State *L)
151 Task tk = checkTask(L,1);
152 lua_pushnumber(L,MSG_task_get_compute_duration (tk));
158 *Function to Excute a Task
161 static int Task_execute(lua_State *L)
163 Task tk = checkTask(L,1);
164 int res = MSG_task_execute(tk);
165 lua_pushnumber(L,res);
170 *Function ro Desroy a Task
172 static int Task_destroy(lua_State *L)
174 Task tk = checkTask(L,1);
175 int res = MSG_task_destroy(tk);
176 lua_pushnumber(L,res);
185 //*************************************************************** HOST SECTION ***************************************************************************
189 static Host toHost(lua_State *L,int index)
191 Host *pi = (Host*) lua_touserdata(L,index);
192 if(pi == NULL) luaL_typerror(L,index,HOST);
198 static Host checkHost (lua_State *L,int index)
201 luaL_checktype(L,index,LUA_TUSERDATA);
202 pi = (Host*)luaL_checkudata(L,index,HOST);
203 if(pi == NULL ) luaL_typerror(L,index,HOST);
206 luaL_error(L,"null Host");
212 static Host *pushHost (lua_State *L,Host ht)
214 Host *pi = (Host*)lua_newuserdata(L,sizeof(Host));
216 luaL_getmetatable(L,HOST);
217 lua_setmetatable(L,-2);
223 static int Host_new(lua_State* L)
225 const char *host_name=luaL_checkstring(L,1);
226 pushHost(L,MSG_get_host_by_name(host_name));
232 static int Host_name(lua_State* L)
234 Host ht = checkHost(L,1);
235 lua_pushstring(L,MSG_host_get_name(ht));
240 //******************************************************* PROCESS SECTION ***************************************************************************
241 static Process toProcess(lua_State *L,int index)
243 Process *pi = (Process*) lua_touserdata(L,index);
244 if(pi == NULL) luaL_typerror(L,index,PROCESS);
250 static Process checkProcess (lua_State *L,int index)
253 luaL_checktype(L,index,LUA_TUSERDATA);
254 pi = (Process*)luaL_checkudata(L,index,PROCESS);
255 if(pi == NULL ) luaL_typerror(L,index,PROCESS);
258 luaL_error(L,"null Process");
264 static Process *pushProcess (lua_State *L,Process ps)
266 Process *pi = (Process*)lua_newuserdata(L,sizeof(Process));
268 luaL_getmetatable(L,PROCESS);
269 lua_setmetatable(L,-2);
275 m_process_t MSG_process_create ( const char * name,
276 xbt_main_func_t code,
284 static int Process_new(lua_State* L)
286 const char *name=luaL_checkstring(L,1);
287 //int code = luaL_checkint(L,2);
288 //xbt_main_func_t << code
289 // We Will Set The Data as a Null for The Moment
290 Host ht = checkHost(L,4);
291 pushProcess(L,MSG_process_create(name,NULL,NULL,ht));
297 static int Process_kill(lua_State *L)
299 Process ps = checkProcess (L,1);
300 MSG_process_kill(ps);
306 //**********************************************************MSG Operating System Functions******************************************************************
308 * Function to Send a Task
310 static int Task_send(lua_State *L) {
311 Task tk = checkTask(L,1);
312 const char *mailbox = luaL_checkstring(L,2);
313 stackDump(L);fflush(stdout);fflush(stderr);
314 int res = MSG_task_send(tk,mailbox);
315 res++;//FIXME: check it instead of avoiding the warning
320 * Function Recieve a Task
322 static int Task_recv(lua_State *L) {
325 const char *mailbox = luaL_checkstring(L,1);
326 int res = MSG_task_receive(&tk,mailbox);
327 res++;//FIXME: check it instead of avoiding the warning
328 INFO1("Task Name : >>>%s",MSG_task_get_name(tk));
334 //******************************************************* PLATFORM & APPLICATION SECTION ****************************************************************
337 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WE WONT NEED IT (!!??)
339 * Launch Application Function
342 static int launch_application(lua_State *L)
345 const char * file = luaL_checkstring(L,1);
346 MSG_launch_application(file);
352 * Create Environment Function
355 static int create_environment(lua_State *L)
358 const char *file = luaL_checkstring(L,1);
359 MSG_create_environment(file);
365 //****************************************************************** REGISTERING ************************************************************
367 *Registering Into Lua
371 static const luaL_reg Task_methods[] = {
375 {"execute", Task_execute},
376 {"destroy", Task_destroy},
383 static const luaL_reg Host_methods[] = {
390 static const luaL_reg Process_methods[] = {
391 {"new", Process_new},
392 {"kill", Process_kill},
402 static int Task_gc(lua_State *L)
405 if (tk) MSG_task_destroy(tk);
406 //printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
410 static int Task_tostring(lua_State *L)
412 lua_pushfstring(L,"Task :%p",lua_touserdata(L,1));
417 static const luaL_reg Task_meta[] = {
419 {"__tostring", Task_tostring},
429 static int Host_gc(lua_State *L)
433 printf("GoodBye Host(%p)\n",lua_touserdata(L,1));
437 static int Host_tostring(lua_State *L)
439 lua_pushfstring(L,"Host :%p",lua_touserdata(L,1));
444 static const luaL_reg Host_meta[] = {
446 {"__tostring", Host_tostring},
454 static int Process_gc(lua_State *L)
456 Process ps=toProcess(L,1);
457 if (ps) MSG_process_kill(ps) ;
458 printf("GoodBye Process(%p)\n",lua_touserdata(L,1));
462 static int Process_tostring(lua_State *L)
464 lua_pushfstring(L,"Process :%p",lua_touserdata(L,1));
469 static const luaL_reg Process_meta[] = {
470 {"__gc", Process_gc},
471 {"__tostring", Process_tostring},
477 *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.
481 * Register Task, Host, and Process
484 int MSG_register(lua_State *L);// FIXME: better location
485 int MSG_register(lua_State *L) {
486 luaL_openlib(L,TASK,Task_methods,0); //create methods table,add it to the globals
487 luaL_newmetatable(L,TASK); //create metatable for Task,add it to the Lua registry
488 luaL_openlib(L,0,Task_meta,0);// fill metatable
489 lua_pushliteral(L,"__index");
490 lua_pushvalue(L,-3); //dup methods table
491 lua_rawset(L,-3); //matatable.__index = methods
492 lua_pushliteral(L,"__metatable");
493 lua_pushvalue(L,-3); //dup methods table
494 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
495 lua_pop(L,1); //drop metatable
497 luaL_openlib(L,HOST,Host_methods,0);
498 luaL_newmetatable(L,HOST);
499 luaL_openlib(L,0,Host_meta,0);
500 lua_pushliteral(L,"__index");
503 lua_pushliteral(L,"__metatable");
508 luaL_openlib(L,PROCESS,Process_methods,0);
509 luaL_newmetatable(L,PROCESS);
510 luaL_openlib(L,0,Host_meta,0);
511 lua_pushliteral(L,"__index");
514 lua_pushliteral(L,"__metatable");