Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Print the Lua version at startup
Browse files Browse the repository at this point in the history
Closes #692.
  • Loading branch information
christopho committed Jun 27, 2015
1 parent b6fde27 commit 3e3096b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -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).

_______________________________

Expand Down
1 change: 1 addition & 0 deletions include/solarus/lua/LuaContext.h
Expand Up @@ -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(
Expand Down
41 changes: 41 additions & 0 deletions src/lua/LuaContext.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e3096b

Please sign in to comment.