1 /* SimGrid Lua bindings */
3 /* Copyright (c) 2010, the SimGrid team. All right reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <lua5.1/lauxlib.h>
10 #include <lua5.1/lualib.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua,bindings,"Lua Bindings");
17 #define TASK_MODULE_NAME "simgrid.Task"
19 /* ********************************************************************************* */
20 /* helper functions */
21 /* ********************************************************************************* */
23 static void stackDump (lua_State *L) {
27 int top = lua_gettop(L);
30 p+=sprintf(p,"STACK(top=%d): ",top);
31 for (i = 1; i <= top; i++) { /* repeat for each level */
32 int t = lua_type(L, i);
35 case LUA_TSTRING: /* strings */
36 p+=sprintf(p,"`%s'", lua_tostring(L, i));
39 case LUA_TBOOLEAN: /* booleans */
40 p+=sprintf(p,lua_toboolean(L, i) ? "true" : "false");
43 case LUA_TNUMBER: /* numbers */
44 p+=sprintf(p,"%g", lua_tonumber(L, i));
47 default: /* other values */
48 if ((ptr = luaL_checkudata(L,i,TASK_MODULE_NAME))) {
51 p+=printf(p,"%s", lua_typename(L, t));
56 p+=sprintf(p," "); /* put a separator */
61 /** @brief ensures that a userdata on the stack is a task and returns the pointer inside the userdata */
62 static m_task_t checkTask (lua_State *L,int index) {
64 luaL_checktype(L,index,LUA_TUSERDATA);
65 pi = (m_task_t*)luaL_checkudata(L,index,TASK_MODULE_NAME);
67 luaL_typerror(L,index,TASK_MODULE_NAME);
70 luaL_error(L,"null Task");
74 /** @brief leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata */
75 static m_task_t *pushTask (lua_State *L,m_task_t tk) {
76 m_task_t *pi = (m_task_t*)lua_newuserdata(L,sizeof(m_task_t));
78 luaL_getmetatable(L,TASK_MODULE_NAME);
79 lua_setmetatable(L,-2);
85 /* ********************************************************************************* */
86 /* wrapper functions */
87 /* ********************************************************************************* */
89 static int Task_new(lua_State* L) {
90 const char *name=luaL_checkstring(L,1);
91 int comp_size = luaL_checkint(L,2);
92 int msg_size = luaL_checkint(L,3);
93 // FIXME: data shouldn't be NULL I guess
94 pushTask(L,MSG_task_create(name,comp_size,msg_size,NULL));
95 INFO0("Created task");
99 static int Task_get_name(lua_State *L) {
100 m_task_t tk = checkTask(L,1);
101 lua_pushstring(L,MSG_task_get_name(tk));
105 static int Task_computation_duration(lua_State *L){
106 m_task_t tk = checkTask(L,1);
107 lua_pushnumber(L,MSG_task_get_compute_duration (tk));
111 static int Task_execute(lua_State *L){
112 m_task_t tk = checkTask(L,1);
113 int res = MSG_task_execute(tk);
114 lua_pushnumber(L,res);
118 static int Task_destroy(lua_State *L) {
119 m_task_t tk = checkTask(L,1);
120 int res = MSG_task_destroy(tk);
121 lua_pushnumber(L,res);
124 static int Task_send(lua_State *L) {
125 m_task_t tk = checkTask(L,1);
126 const char *mailbox = luaL_checkstring(L,2);
127 stackDump(L);fflush(stdout);fflush(stderr);
128 int res = MSG_task_send(tk,mailbox);
129 res++;//FIXME: check it instead of avoiding the warning
133 static int Task_recv(lua_State *L) {
136 const char *mailbox = luaL_checkstring(L,1);
137 int res = MSG_task_receive(&tk,mailbox);
138 res++;//FIXME: check it instead of avoiding the warning
139 INFO1("Task Name : >>>%s",MSG_task_get_name(tk));
145 static const luaL_reg Task_methods[] = {
147 {"name", Task_get_name},
148 {"computation_duration", Task_computation_duration},
149 {"execute", Task_execute},
150 {"destroy", Task_destroy},
155 static int Task_gc(lua_State *L) {
156 m_task_t tk=checkTask(L,1);
157 if (tk) MSG_task_destroy(tk);
158 //printf("GoodBye Task(%p)\n",lua_touserdata(L,1));
162 static int Task_tostring(lua_State *L) {
163 lua_pushfstring(L, "Task :%p",lua_touserdata(L,1));
168 static const luaL_reg Task_meta[] = {
170 {"__tostring", Task_tostring},
175 * Environment related
177 static int launch_application(lua_State *L) {
178 const char * file = luaL_checkstring(L,1);
179 MSG_launch_application(file);
182 #include "simix/simix.h" //FIXME: KILLME when debugging on simix internals become useless
183 static int create_environment(lua_State *L) {
184 const char *file = luaL_checkstring(L,1);
185 INFO1("Loading environment file %s",file);
186 MSG_create_environment(file);
187 smx_host_t *hosts = SIMIX_host_get_table();
189 for (i=0;i<SIMIX_host_get_number();i++) {
190 INFO1("We have an host %s", SIMIX_host_get_name(hosts[i]));
195 static int debug(lua_State *L) {
196 const char *str = luaL_checkstring(L,1);
200 static int info(lua_State *L) {
201 const char *str = luaL_checkstring(L,1);
205 static int run(lua_State *L) {
209 static int clean(lua_State *L) {
213 static const luaL_Reg simgrid_funcs[] = {
214 { "create_environment", create_environment},
215 { "launch_application", launch_application},
221 { "platform", create_environment},
222 { "application", launch_application},
226 /* ********************************************************************************* */
227 /* module management functions */
228 /* ********************************************************************************* */
230 void SIMIX_ctx_lua_factory_set_state(void *state);/* Hack: pass the L to our factory */
231 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
233 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
235 int luaopen_simgrid(lua_State* L); // Fuck gcc: we don't need that prototype
236 int luaopen_simgrid(lua_State* L) {
237 xbt_ctx_factory_to_use = "lua";
239 char **argv=malloc(sizeof(char*)*LUA_MAX_ARGS_COUNT);
241 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? */
242 /* Get the command line arguments from the lua interpreter */
243 lua_getglobal(L,"arg");
244 xbt_assert1(lua_istable(L,-1),"arg parameter is not a table but a %s",lua_typename(L,-1));
248 lua_pushinteger(L,argc-2);
250 if (lua_isnil(L,-1)) {
253 xbt_assert1(lua_isstring(L,-1),"argv[%d] got from lua is no string",argc-1);
254 xbt_assert2(argc<LUA_MAX_ARGS_COUNT,
255 "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",
256 __FILE__,LUA_MAX_ARGS_COUNT-1);
257 argv[argc-1] = (char*)luaL_checkstring(L,-1);
259 INFO1("Got command line argument %s from lua",argv[argc-1]);
264 /* Initialize the MSG core */
265 MSG_global_init(&argc,argv);
266 INFO1("Still %d arguments on command line",argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
268 /* register the core C functions to lua */
269 luaL_register(L, "simgrid", simgrid_funcs);
270 /* register the tasks to lua */
271 luaL_openlib(L,TASK_MODULE_NAME,Task_methods,0); //create methods table,add it to the globals
272 luaL_newmetatable(L,TASK_MODULE_NAME); //create metatable for Task,add it to the Lua registry
273 luaL_openlib(L,0,Task_meta,0);// fill metatable
274 lua_pushliteral(L,"__index");
275 lua_pushvalue(L,-3); //dup methods table
276 lua_rawset(L,-3); //matatable.__index = methods
277 lua_pushliteral(L,"__metatable");
278 lua_pushvalue(L,-3); //dup methods table
279 lua_rawset(L,-3); //hide metatable:metatable.__metatable = methods
280 lua_pop(L,1); //drop metatable
282 /* Keep the context mechanism informed of our lua world today */
283 SIMIX_ctx_lua_factory_set_state(L);