Anyway, the first portability fix would be to make the same code compile
against Lua 5.1 and 5.2 like a lot of other modules:
LUALIB_API int luaopen_mathx(lua_State *L)
{
#if LUA_VERSION_NUM <= 501
luaL_register(L,LUA_MATHLIBNAME,R);
#else
lua_getglobal(L,LUA_MATHLIBNAME);
luaL_setfuncs(L,R,0);
#endif
Have you checked lmathx for 5.1? It does that already:
http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/5.1/lmathx.tar.gz
That version of lmathx calls luaL_register. The 5.2 version calls
luaL_setfuncs. I do not see a version that combines them together
in the same source file and separates the Lua 5.1 and 5.2 builds
with one #ifdef.
[