From: Christian Heinrich Date: Fri, 6 Mar 2015 13:37:42 +0000 (+0100) Subject: First series of changes to lua-bindings in order to X-Git-Tag: v3_13~1644^2~38 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/17c6e831aec2cc024154308cbb46bd7c5965a599 First series of changes to lua-bindings in order to be capable of running Lua 5.3 --- diff --git a/src/bindings/lua/lua_comm.c b/src/bindings/lua/lua_comm.c index 138c3099eb..5ae312f05f 100644 --- a/src/bindings/lua/lua_comm.c +++ b/src/bindings/lua/lua_comm.c @@ -139,7 +139,7 @@ static int l_comm_test(lua_State* L) { } } -static const luaL_reg comm_functions[] = { +static const luaL_Reg comm_functions[] = { {"wait", l_comm_wait}, {"test", l_comm_test}, /* TODO waitany, testany */ @@ -164,7 +164,7 @@ static int l_comm_gc(lua_State* L) /** * \brief Metamethods of the comm userdata. */ -static const luaL_reg comm_meta[] = { +static const luaL_Reg comm_meta[] = { {"__gc", l_comm_gc}, {NULL, NULL} }; @@ -178,17 +178,22 @@ static const luaL_reg comm_meta[] = { */ void sglua_register_comm_functions(lua_State* L) { - /* create a table simgrid.com and fill it with com functions */ - luaL_openlib(L, COMM_MODULE_NAME, comm_functions, 0); + /* 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 */ /* fill the metatable */ - luaL_openlib(L, NULL, comm_meta, 0); + 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); + /*lua_pushvalue(L, -2);*/ /* simgrid.comm mt simgrid.comm */ /* metatable.__index = simgrid.comm * we put the comm functions inside the comm itself: diff --git a/src/bindings/lua/lua_host.c b/src/bindings/lua/lua_host.c index 3084966fab..eb3e089885 100644 --- a/src/bindings/lua/lua_host.c +++ b/src/bindings/lua/lua_host.c @@ -30,7 +30,7 @@ msg_host_t sglua_check_host(lua_State * L, int index) lua_getfield(L, index, "__simgrid_host"); pi = (msg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME); if (pi == NULL) - luaL_typerror(L, index, HOST_MODULE_NAME); + XBT_ERROR("luaL_checkudata() returned NULL"); ht = *pi; if (!ht) luaL_error(L, "null Host"); @@ -188,7 +188,7 @@ static int l_host_destroy(lua_State *L) return 0; } -static const luaL_reg host_functions[] = { +static const luaL_Reg host_functions[] = { {"get_by_name", l_host_get_by_name}, {"name", l_host_get_name}, {"number", l_host_number}, @@ -217,7 +217,7 @@ static int l_host_tostring(lua_State * L) return 1; } -static const luaL_reg host_meta[] = { +static const luaL_Reg host_meta[] = { {"__tostring", l_host_tostring}, {0, 0} }; @@ -232,24 +232,35 @@ static const luaL_reg host_meta[] = { void sglua_register_host_functions(lua_State* L) { /* create a table simgrid.host and fill it with host functions */ - luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0); + lua_newtable(L); + luaL_setfuncs(L, host_functions, 0); + // Not sure we really need this one here... + /*lua_pushvalue(L, -1);*/ + /*lua_setglobal(L, HOST_MODULE_NAME);*/ + + /*luaL_openlib(L, HOST_MODULE_NAME, host_functions, 0);*/ /* simgrid.host */ /* create the metatable for host, add it to the Lua registry */ luaL_newmetatable(L, HOST_MODULE_NAME); /* simgrid.host mt */ /* fill the metatable */ - luaL_openlib(L, NULL, host_meta, 0); + luaL_setfuncs(L, host_meta, 0); + /*luaL_openlib(L, NULL, host_meta, 0);*/ /* simgrid.host mt */ + /** + * Copy the table and push it onto the stack. + * Required for the lua_setfield call below. + */ lua_pushvalue(L, -2); /* 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"); + lua_setfield(L, -1, "__index"); /* simgrid.host mt */ - lua_pop(L, 2); + lua_setmetatable(L, -2); /* -- */ } diff --git a/src/bindings/lua/lua_platf.c b/src/bindings/lua/lua_platf.c index ac6a80aab2..90e62c6f24 100644 --- a/src/bindings/lua/lua_platf.c +++ b/src/bindings/lua/lua_platf.c @@ -26,7 +26,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_platf, bindings, "Lua bindings (platform mod /* simgrid.platf API */ /* ********************************************************************************* */ -static const luaL_reg platf_functions[] = { +static const luaL_Reg platf_functions[] = { {"open", console_open}, {"close", console_close}, {"AS_open", console_AS_open}, @@ -559,7 +559,11 @@ int console_host_set_property(lua_State *L) { */ void sglua_register_platf_functions(lua_State* L) { - luaL_openlib(L, PLATF_MODULE_NAME, platf_functions, 0); + 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); } diff --git a/src/bindings/lua/lua_process.c b/src/bindings/lua/lua_process.c index 4ce814f6cd..c6698314c3 100644 --- a/src/bindings/lua/lua_process.c +++ b/src/bindings/lua/lua_process.c @@ -45,7 +45,7 @@ static int l_process_sleep(lua_State* L) } } -static const luaL_reg process_functions[] = { +static const luaL_Reg process_functions[] = { {"sleep", l_process_sleep}, /* TODO: self, create, kill, suspend, is_suspended, resume, get_name, * get_pid, get_ppid, migrate @@ -59,7 +59,11 @@ static const luaL_reg process_functions[] = { */ void sglua_register_process_functions(lua_State* L) { - luaL_openlib(L, PROCESS_MODULE_NAME, process_functions, 0); + 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); } diff --git a/src/bindings/lua/lua_state_cloner.c b/src/bindings/lua/lua_state_cloner.c index 93d8271b48..12f552fc44 100644 --- a/src/bindings/lua/lua_state_cloner.c +++ b/src/bindings/lua/lua_state_cloner.c @@ -464,8 +464,12 @@ static void sglua_copy_function(lua_State* src, lua_State* dst) { buffer.size = 0; buffer.data = xbt_new(char, buffer.capacity); - /* copy the binary chunk from src into a buffer */ - XBT_ATTRIB_UNUSED int error = lua_dump(src, sglua_memory_writer, &buffer); + /* copy the binary chunk from src into a buffer + * c.heinrich: Added parameter TRUE for Lua 5.3 - this strips all debug + * information from the function. + */ + // Was before merge: XBT_GNUC_UNUSED and was replaced with XBT_ATTRIB_UNUSED + XBT_ATTRIB_UNUSED int error = lua_dump(src, sglua_memory_writer, &buffer, TRUE); xbt_assert(!error, "Failed to dump the function from the source state: error %d", error); XBT_DEBUG("Fonction dumped: %zu bytes", buffer.size); @@ -506,7 +510,7 @@ static void sglua_copy_userdata(lua_State* src, lua_State* dst) { /* copy the data */ /* src: ... udata dst: ... */ - size_t size = lua_objlen(src, -1); + size_t size = lua_rawlen(src, -1); void* src_block = lua_touserdata(src, -1); void* dst_block = lua_newuserdata(dst, size); /* dst: ... udata */ @@ -576,14 +580,14 @@ static int l_get_from_maestro(lua_State *L) { /* want a global or a registry value? */ int pseudo_index; - if (lua_equal(L, 1, LUA_REGISTRYINDEX)) { + if (lua_compare(L, 1, LUA_REGISTRYINDEX, LUA_OPEQ)) { /* registry */ pseudo_index = LUA_REGISTRYINDEX; XBT_DEBUG("Will get the value from the registry of maestro"); } else { /* global */ - pseudo_index = LUA_GLOBALSINDEX; + pseudo_index = lua_getfield(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); XBT_DEBUG("Will get the value from the globals of maestro"); } @@ -655,7 +659,8 @@ lua_State* sglua_clone_maestro(void) { lua_setmetatable(L, -2); /* thread newenv mt reg */ lua_pop(L, 1); /* thread newenv mt */ lua_setmetatable(L, -2); /* thread newenv */ - lua_setfenv(L, -2); /* thread */ + // TODO c.heinrich This needs to be re-implemented + /*lua_setfenv(L, -2); [> thread <]*/ lua_pop(L, 1); /* -- */ /* create the table of known tables from maestro */ diff --git a/src/bindings/lua/lua_task.c b/src/bindings/lua/lua_task.c index 89a952eea9..140229880a 100644 --- a/src/bindings/lua/lua_task.c +++ b/src/bindings/lua/lua_task.c @@ -61,8 +61,8 @@ static int l_task_new(lua_State* L) { XBT_DEBUG("Task new"); const char* name = luaL_checkstring(L, 1); - int comp_size = luaL_checkint(L, 2); - int msg_size = luaL_checkint(L, 3); + int comp_size = (int) luaL_checkinteger(L, 2); + int msg_size = (int) luaL_checkinteger(L, 3); /* name comp comm */ lua_settop(L, 0); /* -- */ @@ -390,7 +390,7 @@ static int l_task_irecv(lua_State* L) return 1; } -static const luaL_reg task_functions[] = { +static const luaL_Reg task_functions[] = { {"new", l_task_new}, {"get_name", l_task_get_name}, {"get_computation_duration", l_task_get_computation_duration}, @@ -440,7 +440,7 @@ static int l_task_tostring(lua_State* L) /** * \brief Metamethods of both a task table and the userdata inside it. */ -static const luaL_reg task_meta[] = { +static const luaL_Reg task_meta[] = { {"__gc", l_task_gc}, /* will be called only for userdata */ {"__tostring", l_task_tostring}, {NULL, NULL} @@ -456,14 +456,19 @@ 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 */ - luaL_openlib(L, TASK_MODULE_NAME, task_functions, 0); + 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 */ /* create the metatable for tasks, add it to the Lua registry */ luaL_newmetatable(L, TASK_MODULE_NAME); /* simgrid.task mt */ /* fill the metatable */ - luaL_openlib(L, NULL, task_meta, 0); + 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 */ diff --git a/src/bindings/lua/lua_utils.c b/src/bindings/lua/lua_utils.c index 7ab708b3b8..914e667a30 100644 --- a/src/bindings/lua/lua_utils.c +++ b/src/bindings/lua/lua_utils.c @@ -167,7 +167,7 @@ void* sglua_checkudata_debug(lua_State* L, int ud, const char* tname) sglua_stack_dump("my_checkudata: ", L); if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) - luaL_typerror(L, ud, tname); + XBT_ERROR("Error: Userdata is NULL, couldn't find metatable or top of stack does not equal element below it."); lua_pop(L, 2); return p; } diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index 69de34e473..4005c88e41 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -276,7 +276,12 @@ 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); + /*luaL_Register(L, "simgrid", simgrid_functions);*/ + lua_newtable(L); + luaL_setfuncs(L, simgrid_functions, 0); + lua_pushvalue(L, -1); + lua_setglobal(L, "simgrid"); + /* simgrid */ /* set a finalizer that cleans simgrid, by adding to the simgrid module a