X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/776cb37c86bfeb0dfe51ff03753ffa4fa51cab25..af8dbc965fd8a420205d29aa50867ecbd0e45d33:/src/bindings/lua/lua_comm.c 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); /* -- */ }