From 3e3096bfa26f3fdd0d959e1d26a7a20308d2bc74 Mon Sep 17 00:00:00 2001 From: Christopho Date: Sat, 27 Jun 2015 22:02:08 +0200 Subject: [PATCH] Print the Lua version at startup Closes #692. --- ChangeLog | 1 + include/solarus/lua/LuaContext.h | 1 + src/lua/LuaContext.cpp | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8c82c55f5..22bbe8002 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ Solarus 1.5.0 (in progress) _______________________________ * Print when the main loop starts and stops. +* Print the Lua version at startup (#692). _______________________________ diff --git a/include/solarus/lua/LuaContext.h b/include/solarus/lua/LuaContext.h index dd4c271d6..03bf894c0 100644 --- a/include/solarus/lua/LuaContext.h +++ b/include/solarus/lua/LuaContext.h @@ -981,6 +981,7 @@ class LuaContext { static void do_file(lua_State* l, const std::string& script_name); static bool do_file_if_exists(lua_State* l, const std::string& script_name); void print_stack(lua_State* l); + void print_lua_version(); // Initialization of modules. void register_functions( diff --git a/src/lua/LuaContext.cpp b/src/lua/LuaContext.cpp index f4d52fec8..8c360e83d 100644 --- a/src/lua/LuaContext.cpp +++ b/src/lua/LuaContext.cpp @@ -104,6 +104,8 @@ void LuaContext::initialize() { lua_atpanic(l, l_panic); luaL_openlibs(l); + print_lua_version(); + // Associate this LuaContext object to the lua_State pointer. lua_contexts[l] = this; @@ -772,6 +774,45 @@ void LuaContext::print_stack(lua_State* l) { std::cout << std::endl; } +/** + * \brief Prints the version of Lua. + * + * This detects if LuaJIT is being used. + */ +void LuaContext::print_lua_version() { + + Debug::check_assertion(lua_gettop(l) == 0, "Non-empty Lua stack before print_lua_version()"); + + // _VERSION is the Lua language version, giving the same + // result for vanilla Lua and LuaJIT. + // But we want to tell the user if LuaJIT is being used. + // To detect this, we can check the presence of the jit table. + std::string version; + // - + lua_getglobal(l, "jit"); + // jit/nil + if (lua_isnil(l, -1)) { + // Vanilla Lua. + // nil + lua_getglobal(l, "_VERSION"); + // nil version + version = LuaTools::check_string(l, -1); + lua_pop(l, 2); + // - + std::cout << "LuaJIT: no (" << version << ")" << std::endl; + } + else { + // LuaJIT. + // jit + version = LuaTools::check_string_field(l, -1, "version"); + lua_pop(l, 1); + // - + std::cout << "LuaJIT: yes (" << version << ")" << std::endl; + } + + Debug::check_assertion(lua_gettop(l) == 0, "Non-empty Lua stack after print_lua_version()"); +} + /** * \brief Defines some C++ functions into a Lua table. * \param module_name name of the table that will contain the functions