From: Christian Heinrich Date: Wed, 11 Mar 2015 14:35:18 +0000 (+0100) Subject: Second series of changes to lua-bindings in order to be capable of running Lua 5.3 X-Git-Tag: v3_13~1644^2~36 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/af8dbc965fd8a420205d29aa50867ecbd0e45d33 Second series of changes to lua-bindings in order to be capable of running Lua 5.3 * Updated lua_*.c files to support again the simgrid.*.* syntax, like simgrid.host.at(i) -- this was broken before. --- diff --git a/src/bindings/lua/lua_comm.c b/src/bindings/lua/lua_comm.c index 5ae312f05f..5e761e6113 100644 --- a/src/bindings/lua/lua_comm.c +++ b/src/bindings/lua/lua_comm.c @@ -179,29 +179,30 @@ static const luaL_Reg comm_meta[] = { void sglua_register_comm_functions(lua_State* L) { /* create a table simgrid.comm and fill it with com functions */ - lua_newtable(L); - luaL_setfuncs(L, comm_functions, 0); - lua_setglobal(L, COMM_MODULE_NAME); - /*luaL_openlib(L, COMM_MODULE_NAME, comm_functions, 0);*/ - /* simgrid.comm */ - - /* create the metatable for comms, add it to the Lua registry */ - luaL_newmetatable(L, COMM_MODULE_NAME); - /* simgrid.comm mt */ + lua_getglobal(L, "simgrid"); /* simgrid */ + luaL_newlib(L, comm_functions); /* simgrid simgrid.comm */ + lua_setfield(L, -2, "comm"); /* simgrid */ + lua_getfield(L, -1, "host"); /* simgrid simgrid.comm */ + + /* create the metatable for comm, add it to the Lua registry */ + luaL_newmetatable(L, COMM_MODULE_NAME); /* simgrid simgrid.comm mt */ + /* fill the metatable */ - luaL_setfuncs(L, comm_meta, 0); - luaL_setmetatable(L, COMM_MODULE_NAME); - /*luaL_openlib(L, NULL, comm_meta, 0);*/ - /* simgrid.comm mt */ - /*lua_pushvalue(L, -2);*/ - /* simgrid.comm mt simgrid.comm */ + luaL_setfuncs(L, comm_meta, 0); /* simgrid simgrid.comm mt */ + + /** + * Copy the table and push it onto the stack. + * Required for the lua_setfield call below. + */ + lua_getfield(L, -3, "comm"); /* simgrid simgrid.comm mt simgrid.comm */ + /* metatable.__index = simgrid.comm - * we put the comm functions inside the comm itself: + * we put the comm functions inside the comm userdata itself: * this allows to write my_comm:method(args) for * simgrid.comm.method(my_comm, args) */ - lua_setfield(L, -2, "__index"); - /* simgrid.comm mt */ - lua_pop(L, 2); - /* -- */ + lua_setfield(L, -2, "__index"); /* simgrid simgrid.comm mt */ + + lua_setmetatable(L, -2); /* simgrid simgrid.comm */ + lua_pop(L, 2); /* -- */ } diff --git a/src/bindings/lua/lua_host.c b/src/bindings/lua/lua_host.c index ae93b68c9e..b9d01a9a73 100644 --- a/src/bindings/lua/lua_host.c +++ b/src/bindings/lua/lua_host.c @@ -52,7 +52,7 @@ static int l_host_get_by_name(lua_State * L) XBT_DEBUG("Getting Host from name..."); msg_host_t msg_host = MSG_host_by_name(name); if (!msg_host) { - luaL_error(L, "null Host : MSG_get_host_by_name failed"); + XBT_ERROR("MSG_get_host_by_name failed, requested hostname: %s", name); } lua_newtable(L); /* create a table, put the userdata on top of it */ msg_host_t *lua_host = (msg_host_t *) lua_newuserdata(L, sizeof(msg_host_t)); @@ -235,26 +235,27 @@ void sglua_register_host_functions(lua_State* L) lua_getglobal(L, "simgrid"); /* simgrid */ luaL_newlib(L, host_functions); /* simgrid simgrid.host */ lua_setfield(L, -2, "host"); /* simgrid */ + lua_getfield(L, -1, "host"); /* simgrid simgrid.host */ /* create the metatable for host, add it to the Lua registry */ - luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid mt */ + luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid simgrid.host mt */ /* fill the metatable */ - luaL_setfuncs(L, host_meta, 0); /* simgrid mt */ + luaL_setfuncs(L, host_meta, 0); /* simgrid simgrid.host mt */ /** * Copy the table and push it onto the stack. * Required for the lua_setfield call below. */ - lua_getfield(L, -2, "host"); /* simgrid mt simgrid.host */ + lua_getfield(L, -3, "host"); /* simgrid simgrid.host mt simgrid.host */ /* metatable.__index = simgrid.host * we put the host functions inside the host userdata itself: * this allows to write my_host:method(args) for * simgrid.host.method(my_host, args) */ - lua_setfield(L, -2, "__index"); /* simgrid mt */ + lua_setfield(L, -2, "__index"); /* simgrid simgrid.host mt */ - lua_setmetatable(L, -2); /* simgrid */ - lua_pop(L, 1); + lua_setmetatable(L, -2); /* simgrid simgrid.host */ + lua_pop(L, 2); /* -- */ } diff --git a/src/bindings/lua/lua_platf.c b/src/bindings/lua/lua_platf.c index 90e62c6f24..1b7d143701 100644 --- a/src/bindings/lua/lua_platf.c +++ b/src/bindings/lua/lua_platf.c @@ -559,12 +559,10 @@ int console_host_set_property(lua_State *L) { */ void sglua_register_platf_functions(lua_State* L) { - lua_newtable(L); - luaL_setfuncs(L, platf_functions, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, PLATF_MODULE_NAME); - /*luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0);*/ - /* simgrid.platf */ - lua_pop(L, 1); + lua_getglobal(L, "simgrid"); /* simgrid */ + luaL_newlib(L, platf_functions); /* simgrid simgrid.platf */ + lua_setfield(L, -2, "platf"); /* simgrid */ + + lua_pop(L, 1); /* -- */ } diff --git a/src/bindings/lua/lua_process.c b/src/bindings/lua/lua_process.c index c6698314c3..ffbcf362c2 100644 --- a/src/bindings/lua/lua_process.c +++ b/src/bindings/lua/lua_process.c @@ -59,12 +59,9 @@ static const luaL_Reg process_functions[] = { */ void sglua_register_process_functions(lua_State* L) { - lua_newtable(L); - luaL_setfuncs(L, process_functions, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, PROCESS_MODULE_NAME); - /*luaL_openlib(L, PROCESS_MODULE_NAME, process_functions, 0);*/ - /* simgrid.process */ - lua_pop(L, 1); + lua_getglobal(L, "simgrid"); /* simgrid */ + luaL_newlib(L, process_functions); /* simgrid simgrid.process */ + lua_setfield(L, -2, "process"); /* simgrid */ + lua_pop(L, 1); /* -- */ } diff --git a/src/bindings/lua/lua_task.c b/src/bindings/lua/lua_task.c index 140229880a..e11aa326db 100644 --- a/src/bindings/lua/lua_task.c +++ b/src/bindings/lua/lua_task.c @@ -456,30 +456,26 @@ static const luaL_Reg task_meta[] = { void sglua_register_task_functions(lua_State* L) { /* create a table simgrid.task and fill it with task functions */ - lua_newtable(L); - luaL_setfuncs(L, task_functions, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, TASK_MODULE_NAME); - /*luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0);*/ - /* simgrid.task */ + lua_getglobal(L, "simgrid"); /* simgrid */ + luaL_newlib(L, task_functions); /* simgrid simgrid.task */ + lua_setfield(L, -2, "task"); /* simgrid */ + lua_getfield(L, -1, "task"); /* simgrid simgrid.task */ /* create the metatable for tasks, add it to the Lua registry */ - luaL_newmetatable(L, TASK_MODULE_NAME); - /* simgrid.task mt */ + luaL_newmetatable(L, TASK_MODULE_NAME); /* simgrid simgrid.task mt */ + /* fill the metatable */ - luaL_setfuncs(L, task_meta, 0); - /*luaL_openlib(L, NULL, task_meta, 0);*/ - /* simgrid.task mt */ - lua_pushvalue(L, -2); - /* simgrid.task mt simgrid.task */ + luaL_setfuncs(L, task_meta, 0); /* simgrid simgrid.task mt */ + lua_getfield(L, -3, "task"); /* simgrid simgrid.task mt simgrid.task */ + /* metatable.__index = simgrid.task * we put the task functions inside the task itself: * this allows to write my_task:method(args) for * simgrid.task.method(my_task, args) */ - lua_setfield(L, -2, "__index"); - /* simgrid.task mt */ - lua_pop(L, 2); - /* -- */ + lua_setfield(L, -2, "__index"); /* simgrid simgrid.task mt */ + + lua_setmetatable(L, -2); /* simgrid simgrid.task */ + lua_pop(L, 2); /* -- */ /* set up MSG to copy Lua tasks between states */ MSG_task_set_copy_callback(task_copy_callback); diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index 4005c88e41..62b088a334 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -276,30 +276,19 @@ lua_State* sglua_get_maestro(void) { static void sglua_register_core_functions(lua_State *L) { /* register the core C functions to lua */ - /*luaL_Register(L, "simgrid", simgrid_functions);*/ - lua_newtable(L); - luaL_setfuncs(L, simgrid_functions, 0); - lua_pushvalue(L, -1); - lua_setglobal(L, "simgrid"); - - /* simgrid */ + luaL_newlib(L, simgrid_functions); /* simgrid */ + lua_pushvalue(L, -1); /* simgrid simgrid */ + lua_setglobal(L, "simgrid"); /* simgrid */ /* set a finalizer that cleans simgrid, by adding to the simgrid module a * dummy userdata whose __gc metamethod calls MSG_clean() */ - lua_newuserdata(L, sizeof(void*)); - /* simgrid udata */ - lua_newtable(L); - /* simgrid udata mt */ - lua_pushcfunction(L, simgrid_gc); - /* simgrid udata mt simgrid_gc */ - lua_setfield(L, -2, "__gc"); - /* simgrid udata mt */ - lua_setmetatable(L, -2); - /* simgrid udata */ - lua_setfield(L, -2, "__simgrid_loaded"); - /* simgrid */ - lua_pop(L, 1); - /* -- */ + lua_newuserdata(L, sizeof(void*)); /* simgrid udata */ + lua_newtable(L); /* simgrid udata mt */ + lua_pushcfunction(L, simgrid_gc); /* simgrid udata mt simgrid_gc */ + lua_setfield(L, -2, "__gc"); /* simgrid udata mt */ + lua_setmetatable(L, -2); /* simgrid udata */ + lua_setfield(L, -2, "__simgrid_loaded"); /* simgrid */ + lua_pop(L, 1); /* -- */ } /**