lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Jun 8, 2009 at 1:11 PM, Patrick Donnelly<batrick@batbytes.com> wrote:
> It's just really inconvenient and tedious (IMO) to open a state, the
> Lua libraries, and then get the _VERSION global.

Furthermore, it is difficult for C to query the _VERSION global of an
unknown version Lua library. After creating a state (luaL_newstate in
5.1, lua_open in 5.0) and then loading the base library
(luaopen_base), it then has to push "_VERSION" (lua_pushstring), and
then comes the interesting part. The value of LUA_GLOBALSINDEX is
different between 5.0 and 5.1 (-10001 in 5.0, -10002 in 5.1), and
lua_getglobal is implemented as a macro rather than an exported
function. A method which doesn't depend on the value of
LUA_GLOBALSINDEX would be to call luaL_loadbuffer with "return
_VERSION" and then lua_[p]call (luaL_dostring or luaL_loadstring might
have been your initial guess, but in 5.0 it wase named lua_dostring
and there is no loadstring). Finally, you need to get the value of the
string, which is lua_tolstring in 5.1 versus lua_tostring in 5.0
(5.1's lua_tostring is implemented as a macro onto lua_tolstring).

The above method is backward compatible to 5.0 and 5.1, but has no
guarantee of being forward compatible with currently unreleased
versions of Lua. If the value of LUA_GLOBALSINDEX changes again, or
there are more API changes, then the method will fail and have to
assume that the version of the unknown library is 5.2 or greater. It
would be convenient to have a function with very simple signature
(e.g. LUA_API int (lua_getversion)(void);) which returns a value like
0x50, 0x51, 0x52, etc, which also comes with some assurance from the
Lua team that the function will remain in the API and not have any
changes to its signature or return value form.